Tuesday 24 June 2014

C++ program to swap two strings

I have given 5 different c++ programs to swap two strings. First program simply swaps the string using third variable and also strcpy() function, second program swaps the two strings without using third variable, third program swaps two strings without using strcpy() functions and fourth program swaps two string using call by reference and last program swap two strings using pointers.


Method 1 : C++ program to swap two strings using third variable
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
    char str1[30],str2[30],temp[30];
    cout<<"Enter String String 1 : ";
    cin>>str1;
    cout<<"Enter String String 2 : ";
    cin>>str2;
    strcpy(temp,str1);
    strcpy(str1,str2);
    strcpy(str2,temp);
    cout<<"After swapping "<<endl;
    cout<<"String 1 : "<<str1;
    cout<<endl<<"String 2 : "<<str2;
    return 0;
}



Method 2 : C++ program to swap two strings without using third variable
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
    char str1[100],str2[100];
    int len1,len2;
    cout<<"Enter String 1 : ";
    cin>>str1;
    cout<<"Enter String 2 : ";
    cin>>str2;
    len1=strlen(str1);
    len2=strlen(str2);
    for(int i=len1+1;i<=len2+len1+1;i++)
        str1[i]=str2[i-len1-1];
    for(int i=0;i<=len1;i++)
        str2[i]=str1[i];
    for(int i=len1;i<=len2+len1+1;i++)
        str1[i-len1]=str1[i+1];
    cout<<"After swapping : "<<endl;
    cout<<"String 1 : "<<str1;
    cout<<endl<<"String 2 : "<<str2;
    return 0;
}



Method 3 : C++ program to swap two strings without using strcpy() functions
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
    char str1[30],str2[30],temp[30];
    cout<<"Enter String String 1 : ";
    cin>>str1;
    cout<<"Enter String String 2 : ";
    cin>>str2;
    for(int i=0;(temp[i]=str1[i])!=0;i++);
    for(int i=0;(str1[i]=str2[i])!=0;i++);
    for(int i=0;(str2[i]=temp[i])!=0;i++);
    cout<<"After swapping "<<endl;
    cout<<"String 1 : "<<str1;
    cout<<endl<<"String 2 : "<<str2;
    return 0;
}




Method 4 : C++ program to swap two strings using call by reference
#include<iostream>
#include<string.h>
using namespace std;
void swap(char (&str1)[30],char (&str2)[30])
{
    char temp[30];
    strcpy(temp,str1);
    strcpy(str1,str2);
    strcpy(str2,temp);
}
int main()
{
    char str1[30],str2[30];
    cout<<"Enter String String 1 : ";
    cin>>str1;
    cout<<"Enter String String 2 : ";
    cin>>str2;
    swap(str1,str2);
    cout<<"After swapping "<<endl;
    cout<<"String 1 : "<<str1;
    cout<<endl<<"String 2 : "<<str2;
    return 0;
}



Method 5 : C++ program to swap two strings using pointers
#include<iostream>
using namespace std;
int main()
{
    char *str1,*str2,*temp;
    str1=new char[20];
    str2=new char[20];
    cout<<"Enter string a1: ";
    cin>>str1;
    cout<<"Enter string a2: ";
    cin>>str2;
    cout<<"Before swapping "<<endl;
    cout<<"String a1 : "<<str1<<endl;
    cout<<"String a2 : "<<str2<<endl;
    temp=str1;
    str1=str2;
    str2=temp;
    cout<<"After swapping "<<endl;
    cout<<"String a1 : "<<str1<<endl;
    cout<<"String a2 : "<<str2<<endl;
    return 0;
}

No comments:

Post a Comment