0
What seems the problem with my code?
#include <iostream> #include <ctype.h> #include <string> #include <algorithm> using namespace std ; int main (){ int i = 0; char b[i]; string a, s; getline(cin, a); for (i = 0; i < a.length(); i++) { b[i] = a[i]; if (isalpha(b[i])) {s = s +b[i];} if (b[i] == ' ') {s = s+ " "; } } reverse(s.begin(), s.end()); cout << s; }
1 Resposta
+ 7
Your char array is declared with size i, which is declared as 0 in the previous line. Therefore you cannot assign anything to b[i]. Based on what you are trying to do, I don’t believe that you need an array for char b. You may simply declare it as a variable to be assigned in your for loop.