Showing posts with label Program to find length of string and concanate two strings. Show all posts
Showing posts with label Program to find length of string and concanate two strings. Show all posts

Tuesday, July 19, 2011

Program to find length of string and concanate two strings

/*Serial No.121     [swami100.cpp]*/

/* 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);
    }