Showing posts with label Concatenate. Show all posts
Showing posts with label Concatenate. Show all posts

C Program To Find Length Of String And Concatenate Two Strings

How to write a C Program To Find Length Of String And Concatenate Two Strings in C Programming 

Solution For C Program :

/* C program which read two strings and concatenate the largest string to the smallest string (among the two strings).*/

#include<stdio.h>
#include<string.h>
#include<conio.h>

int leng(char str[20]);
void concat(char str4[20], char str5[20]);
void main()
{
char str1[20],str2[20],str3[20];
int i,j,len,first,second;
clrscr();
printf("Enter the first string:\n");
gets(str1);

printf("Enter the second string:\n");
gets(str2);
first=leng(str1);
second=leng(str2);
printf("\nLength of 1st string= %d",first);
printf("\nLength of 2nd string= %d",second);

if(first<=second)
    {
    concat(str1,str2);
    }
else
    {
    concat(str2,str1);
    }
getch();
}

int leng(char str[20])
    {
    int i=0;
    while(str[i]!='\0')
        {
        i++;
        }
    return(i);
    }

void concat(char str4[20], char str5[20])
    {
    int i=0,j;
    char str6[20];
    while(str4[i]!='\0')
        {
        str6[i]=str4[i];
        i++;
        }
    j=0;
    while(str5[j]!='\0')
        {
        str6[i]=str5[j];
        i++;j++;
        }
    str6[i]='\0';
    printf("\n\nConcanated string= %s",str6);
    }


You may also learn these C Program/Code :

C Program To Swap Two Numbers Without Using Third Variable

C Program Concatenating Two Strings Into A Third System

How to write a C Program Concatenating Two Strings Into A Third System in C Programming Language ?

Solution For C Program : 

/*C Program Concatenating Two Strings Into A Third System.*/

#include<stdio.h>
void main()
{
char str1[40], str2[40], str3[81];
int k = 0, j = 0;
printf ("\nEnter The First String : ");
scanf ("%s", str1);
printf ("\nEnter The Second String : ");
scanf ("%s", str2);
for (k = 0; str1[k] != '\0'; k++)
str3[k] = str1[k];
for (j = 0; str2[j] != '\0'; j++, k++)
str3[k] = str2[j];
str3[k] = '\0';
printf ("\nThe Two Stings Are : \n\n%s \t %s\n\n", str1, str2);
printf ("\nThe Concatenated Sting is : %s", str3);
}


You may also learn these C Program/Code :

C Program To Swap Two Numbers Without Using Third Variable

C Program to concatenate two strings without using string functions

How to write a C Program to concatenate two strings without using string functions in C Programming Language ?

This C Program to concatenate two strings without using string functions.

Solution:

  1. #include<stdio.h>
  2. #include<conio.h>
  3. void main()
  4. {
  5.     char a[50],b[50];
  6.     int i,j,n,k;
  7.     clrscr();
  8.     printf("\n Please Give The STRING OF A : ");
  9.     scanf("%s",a);
  10.     printf("\n Please Give The STRING OF B : ");
  11.     scanf("%s",b);
  12.     for(i=0,k=0;a[i]!='\0';i++)
  13.         k++;
  14.     for(j=0,n=0;b[j]!='\0';j++)
  15.         n++;
  16.     printf("VALUE OF K=%d",k);
  17.     printf("VALUE OF N=%d",n);
  18.     for(i=k,j=0;i<=n+k;i++,j++)
  19.             a[i]=b[j];
  20.     printf("\n THE Concatenated string is %s .",a);
  21.     getch();
  22. }