C Program to Concatenate Two Strings Lexically

This is a C program to concatenate two strings lexically.

Problem Description

This C Program concatenates the given two strings lexically.

Problem Solution

Take input from the user and concatenates the two strings as shown in the program below.

Program/Source Code

Here is source code of the C Program to concatenate the given two strings lexically. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

/*  
 * C Program to Concatenate the given two Strings Lexically
 */
#include <string.h>
#include <stdio.h>
 
void sort(char *p);
 
void main()
{
    char string1[100], string2[100];
    int i, len, j;
 
    printf("\nEnter a string : ");
    scanf("%[^\n]s", string1);
    printf("\nEnter another string to concat : ");
    scanf(" %[^\n]s", string2);
    len = strlen(string1);
    string1[len] = ' ';
    for(i = 0, j = len + 1; i < strlen(string2); i++, j++)
        string1[j] = string2[i];
    string1[j]='\0';
    sort(string1);
}
 
/* Sorting to make concatenation lexical */
void sort(char *p)
{
    char temp[100];
    char a[100][100];
    int t1, i, j = 0, k = 0, l = strlen(p), x = 0, y = 0, z = 0, count, l1, l2;
 
    for (i = 0; i < l; i++)
    {
        if (p[i] != ' ') 
        {
            a[k][j++] = p[i];
        }
        else
        {
            a[k][j] = '\0';
            k++;
            j = 0;
        }
    }
 
    t1 = k;
    k = 0;
 
    for (i = 0; i < t1; i++)
    {
        for (j = i + 1; j <= t1; j++)
        {
            l1 = strlen(a[i]);
            l2 = strlen(a[j]);
            if (l1 > l2)
                count = l1;
            else
                count = l2;
            x = 0, y = 0;
            while ((x < count) || (y < count))
            {
                if (a[i][x] == a[j][y])
                {
                    x++;
                    y++;
                    continue;
                }
                else 
                    if (a[i][x] < a[j][y]) break;
                else 
                    if (a[i][x] > a[j][y])
                    {
                        for (z = 0; z < l2; z++)
                        {
                            temp[z] = a[j][z];
                            a[j][z] = '\0';
                        }
                        temp[z] = '\0';
 
                        for (z = 0; z < l1; z++)
                        {
                            a[j][z] = a[i][z];
                            a[i][z] = '\0';
                        }
                        a[j][z] = '\0';
 
                        for (z = 0; z < strlen(temp); z++)
                        {
                            a[i][z] = temp[z];
                        }
                        break;
                    }    
                }
            }    
        }
 
    for (i = 0; i < l; i++)
    p[i] = '\0';
    k = 0;
    j = 0;
    for (i = 0; i < l; i++)
    {
        if (a[k][j] != '\0')
        {
            p[i] = a[k][j++];
        }
        else
        {
            k++;
            j = 0;
            p[i] = ' ';
        }
    }
    puts(p);        
}
Program Explanation

In this C program, We are reading two strings using the string1[] and string2[] array variables. The memset() function is used to initialize the value of string to NULL.

advertisement
advertisement

Using for loop simply traverse the value of ‘string1’ variable and assign the value of ‘i’ variable to ‘pos’ variable. Concatenate the second string to the end of the first string.

Another for loop is used to assign the value of ‘string2[]’ array character variable to ‘string1[]’ array variable. Assign the last character of string1 to null and print the concatenated value of the strings.

Runtime Test Cases
 
$ cc string17.c
$ a.out
 
Enter a string : hello this
 
Enter another string to concat : is sanfoundry
hello is sanfoundry this

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at other example programs on Strings, go to C Programming Examples on Strings. If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement
If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.