Lowercase and reverse

0 Tasos Tsichitas · November 20, 2015
any function to convert entered string from uppercase to lowercase and reverse? without using toupper and tolower ..

Post a Reply

Replies

Oldest  Newest  Rating
0 Shreyan Bakshi · November 29, 2015
You can do it using the ASCII codes for the upper and lower case alphabets.
This way, you do not need to use any in-built functions.

\\ ASCII Codes - 'A' to 'Z' is 65 to 90, and 'a' to 'z' is 97 to 122.

#include<iostream>
#include<string>
using namespace std;

int main(){

int ch;

do{

cout << "\nSelect 1 or 2 : ";                      \\ Choose whether you want to convert to Uppercase or to Lowercase.
cout << "\n1. To Upper ";
cout << "\n2. To Lower \n\n";
cin >> ch;

string a;
cout <<"\nEnter a String : ";
cin >> a;

if(ch==1)                                            \\ If you want to convert to Uppercase.
{
for(int i=0; i<a.length(); i++){
if(a.at(i)>=97 && a.at(i)<=122)
  {a.at(i) -= 32;}
}
cout << "\nThe uppercase string is : " << a << "\n";
}

else if (ch==2)                                       \\ If you want to convert to Lowercase.
{
for(int i=0; i<a.length(); i++){
if(a.at(i)>=65 && a.at(i)<=90)
{a.at(i) +=32;}
}
cout << "\nThe lowercase string is : " << a << "\n";
}

else
{cout << "\nERROR - Please Select either 1 or 2. \n";}     \\ If the user does not choose 1 or 2, they will
                                                                  \\ be given an error, and asked to choose again.
}                                                          \\ end of do

while (ch!=1 && ch!=2);

}
+1 Myron Ortiz · November 21, 2015
+1 for @Alf. 


std::string str = "HELLO, WORLD!";
boost::algorithm::to_lower(str);
+2 Alf Nohr · November 20, 2015
What's the problem of using the STL toupper and tolower? However, you can check out the Boost  library, as it got an  algorithm for converting whole strings.
 http://www.boost.org/doc/libs/1_44_0/doc/html/string_algo/usage.html#id2580058
  • 1

C++

107,044 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator