helppppp with string

i wrote a following program to convert alphabets in string with length 100
this way
lowercase alphabets should convert to uppercase alphabets and reverse

i expect this output for this input ADs.....>adS
but my output is a very strange thing!

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<conio.h>
#include<string>
using namespace std;

int main()
{
    string  old_string=" ";
    char new_string[100];
    getline(cin,old_string,'\n');
    for(int i=0;i<old_string.size();i++)
    {
        if(old_string[i]>=65 && old_string[i]<=90)
             new_string[i]=old_string[i]-32;
        else
            new_string[i]=old_string[i]+32;
    }
    for(int j=0;j<=100;j++)
    {
        cout<<new_string[j];
    }
	getch();
    return 0;
}
Last edited on
Line 14: 65-90 are upper case characters. You want to add 32 (not subtract) to make the equivalent lower case character (97-122).

Line 16: Your else condition is everything that is not upper case. You want to explicitly test if a character is upper case (97-122) before subtracting 32 (not adding).



closed account (E0p9LyTq)
Since you are using C++ std:string, why not use the std::transform() function in <algorithm> and toupper()/tolower() functions? There is no need to muck around with C-style strings.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>
#include <algorithm>

int main()
{
   std::cout << "Please enter a string for case-conversion:\n";
   std::cout << "> ";

   std::string strInput;
   std::getline(std::cin, strInput);
   std::cout << '\n';

   transform(strInput.begin(), strInput.end(), strInput.begin(), toupper);
   std::cout << "The string converted to upper case is:\n" << strInput << "\n\n";

   transform(strInput.begin(), strInput.end(), strInput.begin(), tolower);
   std::cout << "The string converted to lower case is:\n" << strInput << "\n\n";
}

Please enter a string for case-conversion:
> ConverT thIS StrINg!

The string converted to upper case is:
CONVERT THIS STRING!

The string converted to lower case is:
convert this string!
closed account (E0p9LyTq)
Use a C++11's range based for loop to walk through your string and convert each string element using toupper()/tolower():

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
#include <iostream>
#include <string>

int main()
{
   std::cout << "Please enter a string for case-conversion:\n";
   std::cout << "> ";

   std::string strInput;
   std::getline(std::cin, strInput);
   std::cout << '\n';

   std::cout << "String before conversion:\n" << strInput << "\n\n";

   // walk through the string so each element can be modified
   for (auto& sElement : strInput)
   {
      if (sElement != toupper(sElement))
      {
         sElement = toupper(sElement);
      }
      else
      {
         sElement = tolower(sElement);
      }
   }

   std::cout << "String after conversion:\n" << strInput << '\n';
}

Please enter a string for case-conversion:
> ConverT thIS StrINg!

String before conversion:
ConverT thIS StrINg!

String after conversion:
cONVERt THis sTRinG!


The STL <string> library is so much easier to use than C-style strings.
You can use isupper(), islower(), toupper() and tolower() to do this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<iostream>
#include <string>

using std::string;
using std::cin;
using std::cout;

int
main()
{
    string str;
    while (getline(cin, str)) {
	for (auto &ch : str) {
	    if (isupper(ch)) {
		ch = tolower(ch);
	    } else if (islower(ch)) {
		ch = toupper(ch);
	    }
	}
	cout << str << '\n';
    }
}


If you want to do it yourself then here's one way. Note that I'm comparing the character to 'A', 'Z', 'a' and 'z' instead of hard-to-understand constants. Also to convert from, say upper case to lower, I've used ch -'A' + 'a'. ch-'A' will convert 'A' to 'Z' into numerical values 0 to 25. Adding 'a' to that will convert to 'a' to 'z'.

1
2
3
4
5
6
7
	for (auto &ch : str) {
	    if (ch >= 'A' && ch <= 'Z') {
		ch = ch - 'A' + 'a';
	    } else if (ch >= 'a' && ch <= 'z') {
		ch = ch - 'a' + 'A';
	    }
	}
Topic archived. No new replies allowed.