Jungle Camping in C | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula gr√°tis
+ 1

Jungle Camping in C

#include <stdio.h> #include<string.h> int main() { char sound[20]; int i=0, word=1, a; char l[]="Grr", t[]="Rawr", s[]="Ssss", b[]="Chirp"; fgets(sound,sizeof (sound),stdin); while(sound[i] != '\0') { if(sound[i]==' ' || sound[i]=='\n' || sound [i]=='\t') { word++; } i++; } for(i=0;i<word;i++) { if(strcmp(sound,l)==0) { printf ("Lion "); } else if(strcmp(sound,t)==0) { printf ("Rawr "); } else if(strcmp(sound,s)==0) { printf ("Snake "); } else if(strcmp(sound,b)==0) { printf ("Bird "); } } return 0; } Pls help

8th May 2020, 2:36 PM
Saaquib Motiwala
8 Respostas
+ 2
Saaquib Motiwala if you dont wanna write split function , i thought of a workaround. you can use pointer to null the sound string after each whitespace but you still increase word size in the loop. With word counter you know total string length. Then strcmp() will compare only up to the null character in the sound string. you can after that offset the sound pointer by size of the animal sound (they are constant size) , so it will jump the null character and strcmp() can compare next word. maybe not optimal way but quick fix and it work Like this: https://code.sololearn.com/cogeF6sRH9ac/#c
8th May 2020, 3:20 PM
Gen2oo
Gen2oo - avatar
0
By this statements : if(strcmp(sound,l)==0) { printf("Lion "); } Here sound contains your total input line which you taken by fgets.. It will not equal to any strings of task.. If input is only one word like ex: Grr, then it outputs Lion.. So slit the input into words then compare by if conditions....
8th May 2020, 2:53 PM
Jayakrishna ūüáģūüá≥
0
Jayakrishnaūüáģūüá≥ Pls can you tell me how to do it as i am a total beginner
8th May 2020, 2:59 PM
Saaquib Motiwala
0
There are many ways.. But Saaquib Motiwala Can you try by this splitting with strtok() function..? Give it a try... And reply https://www.educative.io/edpresso/splitting-a-string-using-strtok-in-c
8th May 2020, 3:38 PM
Jayakrishna ūüáģūüá≥
0
Jayakrishnaūüáģūüá≥ Can you implement this in code pls
8th May 2020, 3:45 PM
Saaquib Motiwala
0
OK. I will. But The link give you an example how to split.. If you take a look at that, you can do it easily.. Since it is code coach, took it as a practice.. Just add 2 lines.. and then reply...
8th May 2020, 4:07 PM
Jayakrishna ūüáģūüá≥
0
Saaquib Motiwala whats the problem in, for giving a try..? OK.. It's upto you.. This is the code you need... #include <stdio.h> #include<string.h> int main() { char sound[20]; char l[]="Grr", t[]="Rawr", s[]="Ssss", b[]="Chirp"; fgets(sound,sizeof (sound),stdin); char *temp = strtok(sound," "); while(temp!=NULL) { if(strcmp(temp,l)==0) { printf ("Lion "); } else if(strcmp(temp,t)==0) { printf ("Rawr "); } else if(strcmp(temp,s)==0) { printf ("Snake "); } else if(strcmp(temp,b)==0) { printf ("Bird "); } temp = strtok(NULL, " "); } return 0; } [need 1more change, Check out your printf output's, are correct or not..?]
8th May 2020, 5:25 PM
Jayakrishna ūüáģūüá≥
0
#include <stdio.h> #include <string.h> int main(){ char *sound; char sounds[100]; scanf("%[^\n]%*c",sounds); sound = strtok(sounds, " "); while(sound != NULL){ if (!strcmp(sound, "Grr")){ printf("Lion "); } else if (!strcmp(sound, "Rawr")){ printf("Tiger "); } else if (!strcmp(sound, "Ssss")){ printf("Snake "); } else{ printf("Bird "); } sound = strtok(NULL, " "); } return 0; } /* Output: Ssss Rawr Chirp Grr Snake Tiger Bird Lion */
27th Aug 2021, 3:56 PM
SIDDIQUE ZEESHAN AZAM
SIDDIQUE ZEESHAN AZAM - avatar