xxxxxxxxxx
char *string_toupper(char *s)
{
int i;
while( s[i] != '\0' )
{
// if character is in lowercase
// then subtract 32
if( s[i] >= 'a' && s[i] <= 'z' )
{
s[i] = s[i] - 32;
}
// increase iterator variable
i++;
}
return (s);
}
xxxxxxxxxx
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str="Hello World";
int i;
cout<<"The String is: \n"<<str;
cout<<endl<<endl;
cout<<"String after uppercase lowercase modification: \n";
for(i=0; i < str.length(); i++)
{
//if its uppercase add to its Ascii code 32 to make lowercase
if(str[i]>='A' && str[i]<='Z')
cout<<char(str[i]+32);
// else if its lowercase subtract 32 to make it upper case
else if(str[i]>='a' && str[i]<='z')
cout<<char(str[i]-32);
// else if its a space or symbol for example just print it as is
else cout<<str[i];
//the reason this works is that the distance in Ascii from uppercase to lowercase
//is standard and constant
}
return 0;
}