0
The same ruslt as required in task The Spy Life, but isn't work.
I wrote this code for task The Spy Life and when I run Test I got the same result as required but I got info that Test crashes. Does anybody know why is this happen? #include <iostream> #include <string> using namespace std; int main() { string str; getline(cin, str); int len=str.length(); int n=len-1; for(int i=0; i<=len; i++) { if (!((str[i]>='a' && str[i]<='z') || (str[i]>='A' && str[i]<='Z') || str[i]==' ')) { str[i]='\0'; } } for(int i=0;i<(len/2);i++) { char temp=str[i]; str[i]=str[n]; str[n]=temp; n=n-1; } cout<<str; return 0; }
1 Odpowiedź
0
Because you don't remove characters that are not a letter or a space from a string.
You just replacing them with zero chracter.
Declare new string and append only valid characters to it.
And you can iterate a source string in reverse order in the first loop then you don't need the second loop.