Convert each letter in string from upper case to lower, and vice versa.

Basically, I need to do a program that lets me count the number of letters, numbers, spaces and special characters in an inputted string. Then, said program should be able to take the upper cases letters from the string to lower case and vice versa.

I'm still trying to remember how to code, and I'm stuck on the last part (the cases part).

Any suggestions and help would be appreciated.

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include<iostream>
#include<string>
#include<cstdlib>

using namespace std;


int main()
{
    //declaration
     char name[100];
     char str[100];
     char str2[100];
     int i, len, countV=0, countC=0, countD=0, countS=0, countO=0;
     char let;
     cout<<"Enter your name: ";
     cin.getline(name, 100);
     cout<<"Enter a string: ";
     cin.getline(str, 100);
     len=strlen(str);
          
     for (i=0;i<len;i++)
     {
         let=str[i]; 
         if(isupper(let))
         {
            tolower(let);
            str2[i]=str2[i]+let;      
         }
         else if(islower(let))
         {
            toupper(let);
            str2[i]=str2[i]+let;           
         }
         else
         str2[i]=str2[i]+let;
         let=tolower(let);
         if(isdigit(let))
            countD++;
         else if(isspace(let))
            countS++;
         else if(ispunct(let))
            countO++;
         else if(let == 'a'||let == 'e'||let == 'i'||let == 'o'||let == 'u')
         {
            countV++;
         }
         else
            countC++;
     }
     
     
     cout<<"\n\nHello, "<<name<<endl
         <<"you have entered: "<<str<<endl
         <<"There are \n"
         <<countC<<" consonant(s) \n"
         <<countV<<" vowel(s) \n"
         <<countD<<" numbers(s) \n"
         <<countS<<" space(s) \n"
         <<countO<<" special character(s) \n"
         <<str2<<endl;
       
    system("pause");
    return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
if(isupper(let))
         {
			str2[i]=tolower(let);     
         }
         else if(islower(let))
         {
			str2[i]=toupper(let);          
         }
         else
	{
		 	str2[i]=str[i];	
	}
Lines 27 and 32 have no effect. tolower and toupper have return values that must be used.

Lines 28, 33, and 36 make no sense. You're taking the current value of str[i] and adding the value of let to it. Presumably those should be: str2[i] = let;
@machinafour
Oh, I didn't realize that it was that simple, thanks

@cire
Yeah, I caught that a while ago. Thanks for pointing it out. :D
Topic archived. No new replies allowed.