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

Compute sin(x)/cos(x) using Taylor series approximation.Program and Algorithm Computer Science Engineering VTU

Taylor series  #include<stdio.h> #include<stdlib.h> #include<math.h> int main() {  int ch,i;  float num,den,x,degree,sum=0,term;  printf("enter 1 for sin(x)\n enter 2 for cos(x)\n");  scanf("%d",&ch);  printf("enter the degree\n");  scanf("%f",&degree);  x=(degree*3.14)/180.0;  switch(ch) {   case 1:num=x;   den=1;   i=2; break;   case 2:num=1;   den=1;   i=1;   break;   default:printf("enter the proper choice\n");   exit(0); }   do {   term=num/den;   num=-num*x*x;   den=den*i*(i+1);   sum=sum+term;   i=i+2; } while(fabs(term)>0.000001);   if(ch==1) {   printf("sin(%f)without using built in function=%f\n",degree,sum);   printf("sin(%f)with using built in function=%f\n",degree,sin(x)); } else {  printf("cos(%f) without using buil...