converting upper to lower case

i am trying to convert the upper case string to lower through the following code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<iostream>
#include<ctype.h>
using namespace std;

int main()
{
        char string1[]="THE";
        char string2[]="the";
        for(int i=0; i!='\0'; i++)
        {
                string1[i] = (tolower(string1[i]));
                string2[i] = (toupper(string2[i]));
        }
                cout << "string1 is " << string1 << endl;
                cout << "string2 is " << string2 << endl;

        return 0;
}


but it is converting the cases of the given strings. can u figure out whats wrong with this code?
This perfectly works for me

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<iostream>
#include<ctype.h>
using namespace std;

int main()
{
        char string1[]= "THE";
        char string2[]= "the";
        for(int i=0; i < 3; i++)
        {
                string1[i] = (tolower(string1[i]));
                string2[i] = (toupper(string2[i]));
        }
                cout << "string1 is " << string1 << endl;
                cout << "string2 is " << string2 << endl;

        system("PAUSE");
        return EXIT_SUCCESS;
}
Try this in your codes:

1
2
3
             string1[i] = tolower(string1[i]);
                cout<<i<<string1<<endl;
                string2[i] = toupper(string2[i]);


There is problem in .... you must find it yourself to be a good coder....
use this:
1
2
3
4
5
6
7
        int j = strlen(string1);
        for(int i=0; i<j; i++)
        {
                string1[i] = tolower(string1[i]);
                string2[i] = toupper(string2[i]);
        }


you must not assign int type to '\0'!!!
The problem, as already noted, is that '\0' is the same as , which is not the length of your strings.

You can stop a the end of a variable-length string by checking to see if the indexed character is not null (or zero):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<iostream>
#include<cctype>  // <-- this is the standard header name
using namespace std;

int main()
{
        char string1[]="THE";
        char string2[]="there";

        for(int i=0; string1[i]!='\0'; i++)
        {
                string1[i] = (tolower(string1[i]));
        }

        for(int i=0; string2[i]!='\0'; i++)  // <-- Notice how I used two loops, one for each string
        {
                string2[i] = (toupper(string2[i]));
        }

        cout << "string1 is " << string1 << endl;  // <-- Watch your indentation
        cout << "string2 is " << string2 << endl;

        return 0;
}


@biplav
Don't teach people to use system("PAUSE").
Last edited on
is it bad ?? I mean SYSTEM ("PAUSE").Because my compiler has it on default
Yes, it is bad. See http://www.cplusplus.com/articles/j3wTURfi/

I didn't mind it for stupid stuff in the past, but seeing as it is such an endemic problem, I now take pains to remind everyone that it is a really, really bad idea.
You can also use a for loop using the strlen function from the cstring header:
1
2
for(int i = 0; i < strlen(string1); i++)
    ..


strlen() gives the number of characters in a cstring excluding the null character.
Last edited on
...you could, but then you would be counting through to find the string length every time you go through the loop. By stopping with the indexed character is null, you do the same thing as strlen(), but only once.

You could also cache the value:
1
2
for (size_t len = strlen(string1), i = 0; i < len; i++)
  ...
I was thinking that the null character is always going to be the last character, otherwise it's not a cstring, but instead just a normal character array.
Topic archived. No new replies allowed.