Recognizing Space character in C++ Program

print space character in c
isspace(c)
how to add space in c programming
how to check space in string in c
how to add space between words in c
detecting space in c
whitespace character
how to count space in string in c
#include<iostream.h>
#include<conio.h>
void main()
{
char ch;
cout<<"Enter a character:";
cin>>ch;
if(ch==32)
cout<<"space";
else if(ch>=65 && ch<=90)
cout<<"upper case letter";
else if(ch>=97 && ch<=122)
cout<<"lower case letter";
else
cout<<"special character entered";
getch();
} 

I need to check whether character entered is lower or upper case letter,special character, digit or space character. 32 is code for space but as I am entering space as ' ' on the console, its not recognizing ' ' as space.

Spaces are ignored by default, use noskipws

#include<iostream>
using namespace std;
int main()
{
char ch;
cout<<"Enter a character:";
cin>>noskipws>>ch;
if(ch==32)
cout<<"space";
else if(ch>=65 && ch<=90)
cout<<"upper case letter";
else if(ch>=97 && ch<=122)
cout<<"lower case letter";
else
cout<<"special character entered";
getchar();
return 0;
} 

Also, if you're adding '' to the space then keep in mind that only the first character is being recognized.

C isspace(), h header file. List of all white-space characters in C programming are: Character � 1. Traverse the given string character by character upto its length, check if character is a whitespace character. 2. If it is a whitespace character, increment the counter by 1, else traverse to the next character. 3. Print the value of the counter.

The problem

cin >> ch discards whitespaces (including space, \t, \n, etc.) The correct way is to use get(ch):

cin.get(ch);

(noskipws is another option mentioned in @Samuel's answer, but get may be easier here for a single character.)

Other problems
  1. Use <iostream> instead of <iostream.h>. <iostream.h> is not standard C++.
  2. Use <cstdio>* instead of <conio.h>. <conio.h> is not standard C++.
  3. Use int main() instead of void main(). void main() is not standard C++.
  4. Use indentation instead of left-justifying. Left-justifying is less readable.
  5. Use ch == ' ' instead of ch == 32. ch == 32 is not portable.
  6. Use isupper(ch) instead of ch >= 65 && ch <= 90. ch >= 65 && ch <= 90 is not portable.
  7. Use islower(ch) instead of ch >= 97 && ch <= 122. ch >= 97 && ch <= 122 is not portable.

Fixed code:

#include <iostream>
#include <cctype>

int main()
{
    char ch;
    std::cout << "Enter a character:";
    std::cin.get(ch);
    if (ch == ' ')
        std::cout << "space";
    else if (std::isupper(ch))
        std::cout << "upper case letter";
    else if (std::islower(ch))
        std::cout << "lower case letter";
    else
        std::cout << "special character entered";
    // std::cin >> ch; // only if you really demand it
}

* Even <cstdio> shouldn't be used in this case. If you do want to hold the window open, use getchar() or std::cin >> ch instead of getch(). The better way is to invoke it in a console.

C library function - isspace(), The C library function int isspace(char c) checks whether the passed character is white-space. Standard white-space characters are − ' ' (0x20) space (� Another way to read string with spaces in C Using fgets() fgets() function requires three parameters. char *s - character pointer (in which string will be stored) int n - maximum number of character of the string; FILE *stream – a pointer of file stream, we can use “stdin”

Use characters themselves instead of their codes.

#include<iostream.h>
#include<conio.h>
void main()
{
    char ch;
    cout << "Enter a character:";
    cin >> ch;
    if (ch == ' ')
        cout << "space";
    else if (ch >= 'A' && ch <= 'Z')
        cout << "upper case letter";
    else if(ch >= 'a' && ch <= 'z')
        cout << "lower case letter";
    else
        cout << "special character entered";
    getch();
}

isspace - C++ Reference, Checks whether c is a white-space character. For the "C" locale, white-space characters are any of: ' ', (0x20), space� This deals with all of the characters below 0x20 (the ASCII code for a space) and above 0x7E (0x7f is the code for DEL, and everything above that is an extension). if (c < ' ' || c >= 0x7f) // Deal with non-printing characters. Pedantic: char c; if (isspace (c)) is UB when c < 0.

isblank - C++ Reference, A blank character is a space character used to separate words within a line of text . The standard "C" locale considers blank characters the tab character ('\t') and� The >> operator will drop leading white space read the target object using white space as a delimiter (so for standard PODs will stop reading the target at the first space). (So it does not 'terminate' at the first white space).

isspace() in C/C++ and its application to count whitespace characters, We use cookies to ensure you have the best browsing experience on our website . By using our site, you acknowledge that you have read and understood our� To elaborate a bit, this printf emits a sequence of bytes: hello worl^H^Hd^J, where ^H is ASCII character #8 and ^J is ASCII character #10. What you see on your screen depends on how your terminal interprets those control codes.

Whitespace character, In computer programming, whitespace is any character or series of characters that represent Most languages only recognize ASCII characters as whitespace, or in some cases Unicode newlines as well, but not most of the characters listed above. The C language defines whitespace characters to be "space, horizontal tab,� In C++, a locale-specific template version of this function exists in header <locale>. Parameters c Character to be checked, casted to an int, or EOF. Return Value A value different from zero (i.e., true) if indeed c is a white-space character. Zero (i.e., false) otherwise. Example

Comments
  • Try printing the value of the character entered.
  • isspace
  • Why are you using numbers instead of characters? Why not just if (ch == ' '), if (ch >= 'A' && ch <= 'Z'), etc?
  • Remove #include <conio.h> (which is a DOS remnant from 3-Decades ago). Instead just use getchar() to hold the terminal window open. 100% portable and doesn't immediately telegraph "I'm really not sure what I'm doing, but this was in the tutorial I'm trying to follow....". And you should really wrap it in conditionals to it is only used on windows to hold the terminal open. E.g. #if defined (_WIN32) || defined (_WIN64) then getchar() followed by #endif.
  • void main is wrong; main must return int.
  • #include<iostream.h> -- you sure, with the '.h'? (that must be a 30 year old compiler...)
  • @DavidC.Rankin OP used that too. Presumably they're using Turbo C++.
  • I just decided to answer the question specifically, assuming their program is running fine. Not sure if it was relevant to talk about iostream.h, void main and the others.
  • Have you added noskipws as I showed in the answer?
  • @AyushiSharma "Turbo C++ is an long obsolete piece of MS-DOS software from 1994 that predates the first version (1998) of the language we call C++ today. Turbo C++ can’t compile C++ programs, and Turbo C++ programs cannot be compiled with C++ compilers." In other words, you need a conforming C++ compiler to compile a standard C++ program; turbo C++ is not one of them. How do you think we are supposed to help you with C++, if you do not even have a standard-conforming C++ compiler beforehand? ;-)
  • This should be a comment.
  • @HolyBlackCat I have used characters earlier before writing ASCII codes