0

how can i fix this?

Hey guys why this code doesn't work. input ==> HEllo WRold output should be = heLLO wrOLD #include <stdio.h> #include <string.h> void ToggleStr(char Str[100] , int aa); int main() { char StrReza[100]; int a = strlen(StrReza); fgets(StrReza,100,stdin); ToggleStr(StrReza , a); return 0; } void ToggleStr(char Str[100] , int aa) { for(int i=0;i<aa;i++) { if(Str[i]>=65 && Str[i]<=90) { Str[i]=Str[i]+32; } else if(Str[i]>=90 && Str[i]<=122) { Str[i]=Str[i]-32; } else { continue; } } printf("%s",Str); }

13th May 2021, 7:19 AM
reza
reza - avatar
2 Réponses
+ 5
The problem is that in the main() function, 'a' is always 0, no matter what the input is. This is because 'StrReza' is empty when you call strlen() on line 6. So it always returns 0, and hence 'a' is always assigned 0. The fix is to move the line where you're assigning length of 'StrReza' after the line where you're taking input. So line 6 & 7 would be ``` fgets(StrReza, 100, stdin); int a = strlen(StrReza); ``` That way you will be calculating length of string after taking the input.
13th May 2021, 7:32 AM
XXX
XXX - avatar
+ 1
@XXX Thanks a lot it's working now
13th May 2021, 7:37 AM
reza
reza - avatar