Strings.

There seems to be an error for some reason and i don't understand why. This program is supposed to convert the text from string s1 to string s2 in Capslock without the spaces.

For example
Input : I see the world
Output : ISEETHEWORLD

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
#include <iostream>
#include <string>
using namespace std;
int main()
{
    int a,b;
    string s1,s2=" ";

    getline(cin, s1);


    for(int i=0;i<20;i++)
    {
        if(s1[i]!=' ')
           {
                if(s1[i]>=97)
                {
                    s2 = s2+(s1[i]-32); //Error here!
                }
                else            {s2=s2+s1[i];}
            }
    }


cout << s2 << endl;
    return 0;
}
@Nison

Try it this way

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
#include <iostream>
#include <string>
using namespace std;
int main()
{
    string s1,s2=" ";

    getline(cin, s1);

int len = s1.length(); // get length of input
    for(int i=0;i<len;i++)
    {
        if(s1[i]!=' ')
           {
                if(s1[i]!='\x20')
                {
                    s2 +=toupper((s1[i]));
                }
                else  
          {s2=s2+s1[i];}
            }
    }


cout << s2 << endl;
    return 0;
}
Last edited on
Try it this way


Yes, it did work. Thank you very much!
Topic archived. No new replies allowed.