cout space in string .

Hello ,

This is my code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main()
{
    string word , word2;
	int n;

     cout<<"Please enter a string :";

	 getline(cin,word);
	 
	 int size=word.length();

	 
	 for (n=0;n<size;n++)
	 {
		 if (islower (word.at(n)))
			 word2.append(1,toupper(word.at(n)));
		      

		 else  if (isupper (word.at(n)))
		     word2.append(1,tolower(word.at(n)));

		 
		 
		 
	 }
	cout<<word2;	
	 cout<<endl;
	 
	 system ("pause");

		 return 0;



}


This is a program use to determine string entered is upper or lower case , and convert them to lower and upper case respectively . The problem is I successfully convert the string but the cout part doesn't cout the spaces entered .

For example :
If I enter my name: "Tak Zee" into the program , it will appear as "tAKzEE" while the space is missing , please help .
Add one more condition in the if-else group to check whether a character is the space character.
Last edited on
You probably want to make that an isalpha check, or something along those lines.

You need to account for other characters that may not be spaces, such as numbers or symbols.
1
2
else if (isspace (word.at(n)))
			 word2.append(" ");


I added this code and it works fine ! Thank you for the tip !
Topic archived. No new replies allowed.