Looking for NEET / JEE Coaching? Enquire Now
You must Sign In to post a response.
  • Category: Programming

    C++ program to perform string concatenation and string comparison.


    Preparing for Diploma Computer Engineering exams? Searching for a program solution to carry out string concatenation and string comparison? Our experts shall provide you the program solution to resolve your query.

    Following is a question that was asked in the question paper of Diploma in Computer Technology(Summer 2019)conducted by Maharashtra State Board of Technical Examination, Mumbai for subject Object Oriented Programming(17432)(G Scheme), Semester-IV

    This question is asked for 8 marks and hence along with program some explanation is also expected.

    Write a program using concept of pointers to string for performing following
    operations :
    (i) String concatenation
    (ii) String comparisons
  • Answers

    1 Answers found.
  • We can perform some operations on strings using C++ programming language. One of them is string concatenation and another one is string comparison. Let's discuss on string concatenation. As the name indicates, it means concatenating two strings. That is joining two strings end to end. + operator is used for concatenating a string. and we cane use & operator also for the concatenation process. An example of string concatenation using pointer is following below :


    #include
    #define MAXIMUMVAL 50
    using namespace std;

    int main() {

    char firstStr[MAXIMUMVAL], secondStr[MAXIMUMVAL];
    char * string1 = firstStr;
    char * string2 = secondStr;
    cout<<"Enter the first string: ";
    cin>>firstStr;
    cout<<"Enter the second string: ";
    cin>>secondStr;
    while(*(++string1));
    while(*(string1++) = *(string2++));
    cout<<"Result of concatenation:"<< firstStr;
    return 0;
    }


    Output :
    Enter the first string: hello
    Enter the second string: world
    Result of concatenation:helloworld

    In the above program, the maximum size of each string is set to 50 using the define directive. Each string that is inputted by the user is assigned to a pointer. The first string is assigned to string1 pointer and the second string is assigned to string2 pointer. while(*(++string1)) statement is used to move to the end of the first string pointer. while(*(string1++) = *(string2++)) is where the copying of second-string happens.

    The other operation on the string is String Comparison. As the name indicates, it is the comparison of two strings. By using string comparison, we can find out whether the two strings are similar or not. We can do the string comparison using pointers in C++ programming language. The below code will help you to understand the concept.


    #include
    #define MAXVAL 10
    using namespace std;
    int main()
    {
    char firstStr[MAXVAL],secondStr[MAXVAL],*string1,*string2;
    cout<<"Enter the first string: ";
    cin>>firstStr;
    cout<<"Enter the second string: ";
    cin>>secondStr;
    string1=firstStr;
    string2=secondStr;
    while(*string1!='\0'||*string2!='\0')
    {
    if(*string1!=*string2)
    {
    cout<<"Result : Not Equal Strings";
    return 0;
    }
    string1++,string2++;
    }
    cout<<"Result : Equal String";
    return 0;
    }

    Output 1 :
    Enter the first string: abc
    Enter the second string: ABC
    Result : Not Equal Strings

    Output 2:
    Enter the first string: abc
    Enter the second string: abc
    Result : Equal String

    In the above code, the maximum length of each string is defined as 10 using the define directive. Each strings will be assigned to pointers. And using a while loop we will be iterating through each character of the string and comparing each other to each one of it equal to another. It's case sensitive.

    --Time is money--
    Benjamin Franklin


  • Sign In to post your comments