0

This is a program to remove all special characters from a string and store the alphabets in a but it doesn't store anything beyond the first space. when the input is "Life is beautiful", the output is "Life".

#include <iostream>
#include <string.h>
using namespace std;
int main(){
string mes="";
string pas;
string tem;
cin>>tem;
cin>>pas;
for (int i=0;i<tem.length();i++){
    char c=tem[i];
    int ch=(int)c;
    if(( ch >= 65 && ch <= 90) || ( ch >= 97 && ch <= 122))
        mes+=c;
    else
        continue;
}
cout<<mes;
  • 3
    `std::cin` reads up to first whitespace character. You may want to use `std::getline` – Yksisarvinen Apr 27 '18 at 13:00
  • 2
    Please don'y use [*magic numbers*](https://en.wikipedia.org/wiki/Magic_number_(programming)). If you by e.g. `65` mean the [ASCII](http://en.cppreference.com/w/cpp/language/ascii) encoded character `'A'` then *say* so (i.e. use `'A'`). Or better yet, use [the standard character classification functions](http://en.cppreference.com/w/cpp/string/byte#Character_classification) (like [`std::isalpha`](http://en.cppreference.com/w/cpp/string/byte/isalpha)). – Some programmer dude Apr 27 '18 at 13:02
  • There's also no need for the `ch` temporary variable, or the `else continue;` part. – Some programmer dude Apr 27 '18 at 13:04
  • Lastly, `` is *not* the file you need for `std::string`. It is the header-file for the C string functions. You want the [``](http://en.cppreference.com/w/cpp/header/string) header file. – Some programmer dude Apr 27 '18 at 13:07

0 Answers0