+ 1

What is wrong with this program?

I have solved this same problem in c++ by using string data type. But in c, i try to solve by using char array pointer. But it shows error. Pls explain me what's the mistake. https://code.sololearn.com/cH3Hj7o5lrkI/?ref=app

4th Apr 2021, 12:13 PM
Jawahirullah
Jawahirullah - avatar
6 Respostas
+ 4
Instead of pointing out how to fix the code, I'll give you the hints to make the correct code: 1. Take the input just like you're doing currently (although I suggest you increase the size of the input string to something more than 30) 2. Use the strtok() function in the <string.h> header to split the string by space. See strtok(): http://www.cplusplus.com/reference/cstring/strtok/ 3. Check each word, print out accordingly just like you're doing currently in your code
4th Apr 2021, 6:24 PM
XXX
XXX - avatar
+ 2
There are many errors in your code. First of all, line 8 is totally bad and unncessary. You are allocating memory for 1 char and then assigning it char array of size 30. This is bad because you are just wasting the memory allocated (even though it's just 1 byte). You don't need line 8. You can simply do `char* p = line` or even better, don't have this line at all and use `line` instead of `p` On line 12, the condition should be `*p != '\0' && *p != ' '` This is because you want to loop as long as *p is not '\0' AND *p is not ' ' The error on line 13 has been pointed out by DHANANJAY PATEL Line 25: this is a disaster here. You are freeing `temp` here, but you are not assigning it any value again. So after the first iteration of the loop, `temp` is actually a pointer to deallocated memory which you are using in places where you shouldn't
4th Apr 2021, 6:22 PM
XXX
XXX - avatar
+ 2
Jerry Hobby Actually, the string has to be split by spaces, and then lines 9-16 (of your code) has to be done on each word of the string. That is why I suggested strtok() in my answer. Also, as it is a code coach here on SoloLearn and solving code coaches gets the person XP, please refrain from giving directly the fixed code in the answer. Instead, point out the mistakes in the OP's code (as you have done) and then only highlight the steps the OP can follow to solve the question. Only if the OP cannot solve even after genuinely trying, then we can give them the fixed code. That's what I feel atleast.
5th Apr 2021, 1:40 AM
XXX
XXX - avatar
+ 1
M. Jawahirullah while(*p!='\0'||*p!=' '){ strcat(temp,*p); p++; } At the first look I can see that strcat() function is called with parameters as bellow strcat(char *, char) strcat(char *, char *) String concatenation requires two string second concatanet at the end of first. DHANANJAY
4th Apr 2021, 12:39 PM
DHANANJAY PATEL
DHANANJAY PATEL - avatar
0
You’ve written a lot of unnecessary code. You only need to allocate enough space for the INPUT. You don’t need to do any string copy functions at all. Just input and compare. Since it’s so close, here’s the cleaner version of what you seem to be trying to do. I am not using malloc() and not using strcpy function at all. Notice how I also limited the fgets to fit the size of the pre-defined line[] variable. This prevents any memory overruns. It has to be -1 to allow for the trailing null. #include<stdio.h> #include<string.h> #include<stdlib.h> int main() { char line[10]; fgets(line,sizeof(line)-1,stdin); if(strcmp(line,"Ssss")==0) printf("Snakes"); else if(strcmp(line,"Chirp")==0) printf("Birds"); else if(strcmp(line,"Rawr")==0) printf("Tigers"); else if(strcmp(line,"Grr")==0) printf("Lions"); return 0; }
4th Apr 2021, 9:45 PM
Jerry Hobby
Jerry Hobby - avatar
0
M Jawahirullah Here is how it is implemented https://www.sololearn.com/coach/45?ref=app sinces it is multiple input of sound of wild animals can be repeated too DHANANJAY
6th Apr 2021, 4:57 PM
DHANANJAY PATEL
DHANANJAY PATEL - avatar