What seems the problem with my code? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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; }

28th Dec 2020, 4:03 AM
Hakam
1 Answer
+ 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.
28th Dec 2020, 4:12 AM
Elizabeth Kelly
Elizabeth Kelly - avatar