Tuesday, 12 April 2016

C And C++ Program to copy one string to another string Problem Solution.


C Solution:

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

    char *str1,*str2;

    str1 = (char*)malloc(sizeof(char));

    printf("Enter any string: ");
    gets(str1);

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

    return 0;
}


C++ Solution:

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

    string str1,str2;

    cout << "Enter any string: ";
    getline(cin,str1);

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

    return 0;
}

No comments:

Post a Comment