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

Write functions to implement string operations such as compare, concatenate, string length. Convince the parameter passing techniques Program and Algorithm Computer Science Engineering VTU

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; ...

How to Calculate VTU Continuous Internal Evaluation(CIE) and Semester End Examination(SEE)

  . Assessment   Details   both   (CIE   and   SEE): The weightage of Continuous Internal Evaluation (CIE) is 50% and for Semester End Exam   (SEE) is 50%. The minimum passing mark for the CIE is 40% of the maximum marks (20   marks).   A   student   shall   be   deemed   to   have   satisfied   the   academic   requirements   and   earned   the   credits allotted to each subject/ course if the student secures not less than 35% (18 Marks out   of   50)   in   the   semester-end   examination(SEE),   and   a   minimum   of   40%   (40   marks   out   of   100)   in   the   sum   total   of   the   CIE   (Continuous   Internal   Evaluation)   and   SEE   (Semester   End   Examination)   taken   together Continuous...