+ 1

the spy life, what is the error in the code?

/*In a test case it should return "tacos" but my code return "∆tacos"*/ #include <stdio.h> #include <string.h> #include <ctype.h> int main() { char a[50],b[50],rev[50]; int c,aux; gets(a); for(int i=0;i<=strlen(a); i++){ if(isalpha(a[i])||a[i]==' '){b[c]=a[i];c++;} } aux=strlen(b); for (int j=0;j<=strlen(b); j++){ rev[j]=b[aux-1]; aux--; } puts(rev); return 0; }

21st Mar 2021, 6:29 AM
Julio Amaro
Julio Amaro - avatar
2 Answers
0
you must first initialize 'c' to zero, and mark the end of the string with null char (zero char code) to get the right length https://code.sololearn.com/cTZdnQ5UWZlM/?ref=app
21st Mar 2021, 7:25 AM
visph
visph - avatar
0
https://code.sololearn.com/cA4vuwo47XPZ/?ref=app • A string must be terminated by NULL (\0) • fgets should be used instead of gets • you didn't intialized c • i and j should be declared outside for loop
21st Mar 2021, 7:50 AM
Kashyap Kumar
Kashyap Kumar - avatar