Skip to main content

STRING concatenation77

STRINGconcatenation

#include<stdio.h>
#include<stdlib.h>
int length (char str[]);
int compare (char str1[],char str2[]);
void concatenate (char str1[],char str2[]);
void main()
{
char str1[30],str2[30];
int choice,a,i,j;
printf("enter 1-string comparision\n");
printf("enter 2-string length\n");
printf("enter 3-string concatenation\n");
printf("enter 4-exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("enter string 1\n");
scanf("%s",str1);
printf("enter string 2\n");
scanf("%s",str2);
a=compare(str1,str2);
if(a==0)
{
printf("%s and%s are identical\n",str1,str2);
}
else
{
printf("%s and%s are not identical\n",str1,str2);
}
break;
case 2:printf("enter a sring\n");
scanf("%s",str1);
a=length(str1);
printf("length of %s=%d\n",str1,a);
break;
case 3: printf("enter string 1\n");
scanf("%s",str1);
printf("enter string 2\n");
scanf ("%s",str2);
concatenate (str1,str2);
break;
case 4:exit(0);
break;
default:printf("enter proper choice\n");
exit(0);
}
}
        int length(char str[])
{
int len=0;
while(str[len]!='\0')
len++;
return len;
}
    int compare(char str1[],char str2[])
{
int i=0;
while (str1[i]==str2[i])
{
if(str1[i]=='\0' || str2[i]=='\0')
break;
i++;
}
return str1[i]-str2[i];
}
    void concatenate (char str1[],char str2[])
{
int i=0,j=0;
while(str1[i]!='\0')
{
i++;
}
while(str2[j]!='\0')
{
str1[i]=str2[j];
i++;
j++;
}
str1[i]='\0';
printf("concatenated string is %s\n",str1);
}

STRINGconcatenation

Start

Declare the required variables

Read choice 1 – String compare, 2 – String length, 3 – String concatenate, 4 - Exit

if choice is equal to 1

Read two strings as str1 and str2

c = compare(str1,str2)

if c ==  0 display identical strings

else display non identical strings

if choice is equal to 2

Read string as str

len = length(str)

Display len

if choice is equal to 3

Read two strings as str1 and str2

str = concatenate(str1,str2)

Display str      

Stop

Algorithm :  String compare

i = 0

while str1[i] == str2[i] do

if str1[i] == ‘\0’ or str2[i] == ‘\0’ break

i = i + 1

return str1[i] – str2[i]

Algorithm : String length

i = 0

while str [i] != ‘\0’ do

i = i + 1

return i

Algorithm : String concatenate

i = 0, j = 0

while str1[i] != ‘\0’ do

i = i + 1

while str2[i] != ‘\0’ do

str1[i] = str2[j]

i = i + 1

j = j + 1

str1[i] = ‘\0’

return str1

Comments

Popular posts from this blog

Raju is a Civil Engineer. He is writing software to automate his work. As a part of his requirement, he wants to calculate the circle diameter, circumference, and area. Help Raju to complete his task. Get radius as input.

OUTPUT

Seetha, a maths teacher has started to learn the C programming language. She writes a program to do basic arithmetic operations. Help Seetha to complete her first program.

OUTPUT