Showing posts with label Program to concanate two strings using pointer and function by passing strings as parameter. Show all posts
Showing posts with label Program to concanate two strings using pointer and function by passing strings as parameter. Show all posts

Tuesday, July 19, 2011

Program to find length of string and concanate two strings using function by passing them as parameter

/*Serial No.117     [swami94.cpp]*/

#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;
clrscr();
printf("Enter the first string:\n");
gets(str1);

printf("Enter the second string:\n");
gets(str2);

concat(str1,str2);

len=leng(str1);
printf("\n\n\nlength of first string using return=%d",len);

getch();
}

int leng(char str[20])
    {
    int i=0;
    while(str[i]!='\0')
    {
    i++;
    }
printf("\n\n\nlength of first string=%d",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\nConcanate string= %s",str6);
    }

Program to concanate two strings using pointer and function by passing strings as parameter

/*Serial No.120     [swami97.cpp]*/

#include<stdio.h>
#include<conio.h>
#include<string.h>
void concat(char *t, char *p);
void main()
{
char str1[10],str2[10];
int l;
clrscr();
printf("Enter the string==>");
gets(str1);
printf("Enter the string==>");
gets(str2);
concat(str1,str2);
getch();
}
void concat(char *s1, char *s2)
    {
    char new_str[20];
    int i=0;
    while(*s1)
        {
        new_str[i]=*s1;
        *s1++;
        i++;
        }
    while(*s2)
        {
        new_str[i]=*s2;
        s2++;
        i++;
        }
    new_str[i]='\0';
    puts(new_str);
    }