2

I wrote a code so that it removes everything(like spaces and other things) other than the alphabats using isalpha() function and converts it to lower case using tolower() function. It is working fine if i don't put a space in the string but if there is any space in the string then it go beyond the space. I dont understand why this is happening. This is the code i wrote.

#include<bits/stdc++.h>
#include<cstring>
#include<cctype>
using namespace std;
int main()
{
    int i;
    string A,b="";
    cin>>A;
    for(i=0;i<A.size();i++)
    {
        if(isalpha(A[i]))
        b+= tolower(A[i]);
        
        else
        continue;
        
    }
    cout<<b;
}

Please help me. Thankyou

purple_tulip
  • 37
  • 1
  • 7

2 Answers2

5

The cin >> A; considers the space to terminate the input.

To get the whole line, use getline(cin, A);

Jiří Baum
  • 6,697
  • 2
  • 17
  • 17
4

cin reads the string till the first space it encounters, if your input string is "Hello World", then cin will only read "Hello".

You can use getline function to read a complete line.

Deepak Patankar
  • 3,076
  • 3
  • 16
  • 35