String and Hackerearth

String Upper:

#include<iostream>
#include<string>
#include<cstdio>
using namespace std;
void upper_string(char st[]);
int main(){
char str[100];
cout<<"Enter string: ";
gets(str);
upper_string(str);
cout<<"upper case:"<<str<<endl;


return 0;
}

void upper_string(char st[])
{
    int c=0;
    while(st[c]!='\0'){
        if(st[c]>='a' && st[c]<='z'){
           st[c]=st[c]-32;
        }
        c++;
    }



}

String Lower:

#include<iostream>
#include<cstdio>
using namespace std;
void lower_string(char st[]);

int main()
{
    char str[500];
    cout<<"Enter: "<<endl;
    gets(str);
    lower_string(str);
    cout<<"Lowercase: "<<str<<endl;
    return 0;
}


void lower_string(char st[])
{
    int c=0;

    while(st[c]!='\0')
    {
        if(st[c]>='A' && st[c]<='Z')
        {
            st[c]=st[c]+32;
        }
        c++;

    }

}

Lower Upper Both:

#include<iostream>
#include<cstdio>
using namespace std;
void lower_upper_string(char st[]);

int main()
{
    char str[500];
    cout<<"Enter: "<<endl;
    gets(str);
    lower_upper_string(str);
    cout<<"case: "<<str<<endl;
    return 0;
}


void lower_upper_string(char st[])
{
    int c=0;

    while(st[c]!='\0')
    {
        //char ch=st[c];
        if(st[c]>='A' && st[c]<='Z')
        {
            st[c]=st[c]+32;
        }else if(st[c]>='a'&&st[c]<='z'){
            st[c]=st[c]-32;
        }
        c++;

    }

}

Hackerearth Problem:: Toggle String

#include <iostream>
#include<cstdio>
#include<string>
using namespace std;
void results(char s[]);
int main()
{
    char str[101];
    gets(str);
    results(str);
    cout<<str;
}

void results(char str[]){
char ch;
int c=0;
while(str[c]!='\0'){
        if(str[c]>='A' && str[c]<='Z'){
            str[c]=str[c]+32;
        }else if(str[c]>='a' && str[c]<='z'){
            str[c]=str[c]-32;
        }
        c++;
    }
}

Another Approach:

#include <iostream>
#include<cstdio>
#include<string>
using namespace std;
void results(char s[]);
int main()
{
    char str[101];
    char ch;
    int c=0;
    gets(str);

while(str[c]!='\0'){
        if(str[c]>='A' && str[c]<='Z'){
            str[c]=str[c]+32;
        }else if(str[c]>='a' && str[c]<='z'){
            str[c]=str[c]-32;
        }
        c++;
    }
       cout<<str;
}

Another Approach:

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int i;
char str[101];

int main()
{
scanf("%s",str);
for(int i=0;i<strlen(str);i++){
    if(str[i]>='A' && str[i]<='Z')
    {
        str[i]=str[i]+32;
    }
    else{
            str[i]=str[i]-32;
    }
}
cout<<str;

return 0;
}

 

Helpful Link: http://www.programmingsimplified.com/c/program/c-program-change-case

It would be a great help, if you support by sharing :)
Author: zakilive

Leave a Reply

Your email address will not be published. Required fields are marked *