Tuesday, 12 April 2016

C And C++ program to copy one string to another string using loop Problem Solution.


C Solution:

#include <stdio.h>
#include<string.h>
 /*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main(){

    char str1[1000],str2[1000];
    int i;
    printf("Enter any string: ");
    gets(str1);

    for(i = 0; str1[i] != '\0'; i++){
        str2[i] = str1[i];
    }
    str2[i] = '\0';

    printf("First String = %s\n", str1);
    printf("Second String = %s\n", str2);

    return 0;
}

C++ Solution:

#include <iostream>
#include<string>
using namespace std;
 /*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main(){

    string str1,str2 = "";
    int i;
    cout << "Enter any string: ";
    getline(cin,str1);

    while(str1[i])str2 += str1[i++];

    cout << "First String = " << str1 << endl;
    cout << "Second String = " << str2 << endl;

    return 0;
}

No comments:

Post a Comment