You must Sign In to post a response.
  • 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

    2 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.

    “It takes a great deal of bravery to stand up to our enemies, but just as much to stand up to our friends."
    – Albus Dumbledore, Harry Potter and the Sorcerer's Stone, Chapter 17

  • (i) Below is the code for concatenation of two string using pointers:

    #include
    using namespace std;
    int main() {
    char string1[10], string2[10];
    char * str1 = string1;
    char * str2 = string2;

    cout<<"Enter first string: ";
    cin>>string1;
    cout<<"\nEnter second string: ";
    cin>>string2;

    while(*(++str1));

    while(*(str1++) = *(str2++));

    cout<<"\nResultant string :"<
    return 0;
    }



    (ii) Below is the code for concatenation of two string using pointers:

    #include
    using namespace std;
    bool stringComparison(char *msg1, char *msg2)
    {
    while(*msg1 == *msg2)
    {
    if(*msg1 == '\0' && *msg2 == '\0')
    {
    return true;
    }
    msg1++;
    msg2++;
    }
    return false;
    }

    int main() {
    // your code goes here
    char string1[] = "IndiaStudyChannel";
    char string2[] = "IndiaStudyChannel";
    if(stringComparison(string1, string2) == 1)
    {
    cout<<"String1 and String2 both are equal";
    }
    else
    {
    cout<<"String1 and String2 are not equal";
    }
    return 0;
    }


  • Sign In to post your comments