Friday 20 June 2014

C++ program to convert the string from upper-case to lower-case and vice versa

Here are different c++ program to convert the string from upper-case to lower-case and vice versa like using for-loop,recursion, function etc.


Method 1 : C++ program to convert the string from upper-case to lower-case
#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
    char str[50];
    cout<<"Enter the string in Uppercase ";
    gets(str);
    cout<<"String in Lower case : ";
    for(int i=0;str[i]!='\0';i++)
          str[i]+=32;
    puts(str);
    return 0;
}

OUTPUT




Method 2 : C++ program to convert upper-case letters of string to lower-case
#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
    char str[50];
    cout<<"Enter the string : ";
    gets(str);
    cout<<"String in Lower case : ";
    for(int i=0;str[i]!='\0';i++)
           if(str[i]>=65&&str[i]<=90)
             str[i]+=32;
    puts(str);
    return 0;
}



Method 3 : C++ program to convert the string from upper-case to lower-case using functions
#include<iostream>
#include<stdio.h>
using namespace std;
void ctol(char (&str)[50])
{
    for(int i=0;str[i]!='\0';i++)
          if(str[i]>=65&&str[i]<=90)
             str[i]+=32;
}
int main()
{
    char str[50];
    cout<<"Enter the string : ";
    gets(str);
    cout<<"String in Lower case : ";
    ctol(str);
    puts(str);
    return 0;
}



Method 4 : C++ program to convert the string from upper-case to lower-case using recursion
#include<iostream>
using namespace std;
void utol(char *str,int i)
{
    if(str[i]=='\0')
        return ;
    else
        if(str[i]>=65&&str[i]<=90)
            cout<<char(str[i]+32);
        else
            cout<<str[i];
        utol(str,i+1);
}
int main()
{
    char str[50];
    cout<<"Enter string : ";
    cin.getline(str,50);
    cout<<"After converting uppercase to lowercase : ";
    utol(str,0);
    return 0;
}



Method 1 : C++ program to convert the string from lower-case to upper-case
#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
    char str[50];
    cout<<"Enter the string in Lowercase ";
    gets(str);
    cout<<"String in Uppercase : ";
    for(int i=0;str[i]!='\0';i++)
          str[i]-=32;
    puts(str);
    return 0;
}

OUTPUT




Method 2 : C++ program to convert the lower-case letters of string to upper-case
#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
    char str[50];
    cout<<"Enter the string : ";
    gets(str);
    cout<<"String in Uppercase : ";
    for(int i=0;str[i]!='\0';i++)
        if(str[i]>=97&&str[i]<=122)
          str[i]-=32;
    puts(str);
    return 0;
}

OUTPUT




Method 3 : C++ program to convert lower-case letters of string to upper-case using function
#include<iostream>
#include<stdio.h>
using namespace std;
void ctou(char (&str)[50])
{
    for(int i=0;str[i]!='\0';i++)
         if(str[i]>=97&&str[i]<=122)
          str[i]-=32;
}
int main()
{
    char str[50];
    cout<<"Enter the string : ";
    gets(str);
    cout<<"String in Uppercase : ";
    ctou(str);
    puts(str);
    return 0;
}



Method 4 : C++ program to convert lower-case letters of string to upper-case using recursion
#include<iostream>
using namespace std;
void ltou(char *str,int i)
{
    if(str[i]=='\0')
        return ;
    else
        if(str[i]>=97&&str[i]<=122)
            cout<<char(str[i]-32);
        else
            cout<<str[i];
        ltou(str,i+1);
}
int main()
{
    char str[50];
    cout<<"Enter string : ";
    cin.getline(str,50);
    cout<<"After converting lowercase to Uppercase : ";
    ltou(str,0);
    return 0;
}

OUTPUT




No comments:

Post a Comment