1 / 4
Mar 2018

How can I read the input as string (with spaces) and deal with it as array of chars?

  • created

    Mar '18
  • last reply

    Dec '22
  • 3

    replies

  • 882

    views

  • 3

    users

  • 1

    like

  • 1

    link

I’ve not used C++ for many years, but this should get you started:

#include <iostream>
#include <string>
using namespace std;

int main() {
    string str;
    while (getline(cin, str)) {
      for (int i=0; i<str.size(); i++)
        printf("%c\n", str[i]);
    }
    return 0;
}
4 years later

You can use gets method in C programs to read string with space characters.
#include <stdio.h>
int main()
{
char str[20];
gets(str);
printf("%s", str);
return 0;
}