Lowest price ever! Learn Generative AI for 48% less!
Get my discount+ 14
BEN_10
scanf will not read space separated string use fgets or scanf("%50[^\n]s", str); 50 for reading max 50 chars. [^\n] read till end of line or \n encountered(whichever occur first)
fgets or scanf will read n-1 char and insert '\n' at nth position, hence '\n' should be removed
str[strlen(str)-1] = '\0';
so replace j= len-1 in last for loop and increase the input string or array size so it can work for all range of inputs
#include <stdio.h>
#include <string.h>
int main() {
char str[2000];
char srr[1000];
int i,j,len,b=0;;
scanf("%100[^\n]s",str);
for(i=0;str[i]!='\0';){
if(((str[i]>='a')&&(str[i]<='z'))||((str[i]>='A')&&(str[i]<='Z'))||(str[i]==' ')){
srr[b]=str[i];
b++;
i++;
}
else {
i++;
}
}
//change b to I in below loop
srr[b]='\0';
// printf ("%s",srr);
len=strlen(srr);
for(j=len-1;j>=0;j--){
printf ("%c",srr[j]);
}
return 0;
}
+ 4
On line 19
srr[i]='\0';
It should be 'b' rather than 'i'
srr[b]='\0';