10. C PROGRAMMING LAB | Check Now
C PROGRAMMING LAB –10] Write functions to implement string operations such as compare, concatenate, string length. Convince the parameter passing techniques.
Algorithm
- Step-1: Start
- Step-2: Press 1 – Compare, 2 – Concatenate, 3-length of string, if press 1 than goto step 3, if press step 2 then goto step 4, if press 3 then goto step 5
- Step-3: Read the string1 and string2 and cojmparsion function by using strcmp built in unction is used. If res = 0 then print both strings are equal, otherwise print strints are not equal and than goto the step 4
- Step-4: Read String1 and sring2 and find concatenation of two strings using string handling function strcat() and display sring and return back to main fuctions.
- Step-5: Read string1 and call function to find the length of string by calling function length ( *string)
- Step-6: if digit=1 then gtoto step 2 else goto step 7
- Step-7: Stop
C PROGRAMMING – Flow Chart


Program -10 source code
#include<stdio.h>
#include<string.h>
void compare(char [ ],char [ ]); .
void concat(char [ ],char [ ]);
void length(char *[ ]);
void main( )
{
int n,digit;
char str1[10],str2[10];
do
{
printf("press 1-compare 2-concatenate 3-length of string");
printf("\n enter your choice=");
scanf("%d",&n);
switch(n)
{
case 1:printf("enter first string=");
scanf("%s",str1);
printf("enter second string=");
scanf("%s",str2);
compare(str1,str2);
break;
case 2: printf("enter first string=");
scanf("%s",str1);
printf("enter second string=");
scanf("%s",str2);
concat(str1,str2);
break;
case 3:printf("enter string=");
scanf("%s",str1);
length(&str1);
break;
default: printf("wrong choice");
break;
}
printf("\n Do you want to continue(1/0)? ");
scanf("%d", &digit);
}while(digit==1);
}
void compare(char str1[ ],char str2[ ])
{
int i;
i=strcmp(str1,str2);
if(i==0)
printf("strings are equal\n ");
else
printf("string are not equal\n");
}
void concat(char str1[ ],char str2[ ])
{
strcat(str1,str2);
printf("concatenate string=%s",str1);
}
void length(char *str1[ ])
{
int len;
len=strlen(str1);
printf("the length of string=%d",len);
}
#include<stdio.h>
#include<string.h>
void compare(char [ ],char [ ]); .
void concat(char [ ],char [ ]);
void length(char *[ ]);
void main( )
{
int n,digit;
char str1[10],str2[10];
do
{
printf("press 1-compare 2-concatenate 3-length of string");
printf("\n enter your choice=");
scanf("%d",&n);
switch(n)
{
case 1:printf("enter first string=");
scanf("%s",str1);
printf("enter second string=");
scanf("%s",str2);
compare(str1,str2);
break;
case 2: printf("enter first string=");
scanf("%s",str1);
printf("enter second string=");
scanf("%s",str2);
concat(str1,str2);
break;
case 3:printf("enter string=");
scanf("%s",str1);
length(&str1);
break;
default: printf("wrong choice");
break;
}
printf("\n Do you want to continue(1/0)? ");
scanf("%d", &digit);
}while(digit==1);
}
void compare(char str1[ ],char str2[ ])
{
int i;
i=strcmp(str1,str2);
if(i==0)
printf("strings are equal\n ");
else
printf("string are not equal\n");
}
void concat(char str1[ ],char str2[ ])
{
strcat(str1,str2);
printf("concatenate string=%s",str1);
}
void length(char *str1[ ])
{
int len;
len=strlen(str1);
printf("the length of string=%d",len);
}
#include<stdio.h> #include<string.h> void compare(char [ ],char [ ]); . void concat(char [ ],char [ ]); void length(char *[ ]); void main( ) { int n,digit; char str1[10],str2[10]; do { printf("press 1-compare 2-concatenate 3-length of string"); printf("\n enter your choice="); scanf("%d",&n); switch(n) { case 1:printf("enter first string="); scanf("%s",str1); printf("enter second string="); scanf("%s",str2); compare(str1,str2); break; case 2: printf("enter first string="); scanf("%s",str1); printf("enter second string="); scanf("%s",str2); concat(str1,str2); break; case 3:printf("enter string="); scanf("%s",str1); length(&str1); break; default: printf("wrong choice"); break; } printf("\n Do you want to continue(1/0)? "); scanf("%d", &digit); }while(digit==1); } void compare(char str1[ ],char str2[ ]) { int i; i=strcmp(str1,str2); if(i==0) printf("strings are equal\n "); else printf("string are not equal\n"); } void concat(char str1[ ],char str2[ ]) { strcat(str1,str2); printf("concatenate string=%s",str1); } void length(char *str1[ ]) { int len; len=strlen(str1); printf("the length of string=%d",len); }
C PROGRAMMING -Viva Questions
1] What is a string?
2] How to declare string?
3] What are the string manipulation functions?
4] What is gets( ) and puts( ) function in string?