Professional Documents
Culture Documents
Worksheet 2.3
Worksheet 2.3
EXPERIMENT NUMBER - 6
PRACTICAL – 2.3.1
AIM OF THE EXPERIMENT – Write a program to illustrate various strings inbuilt functions (strrev,
strcmp, strlen, strcpy, strcat)
ALGORITHM - 1. Start.
2. Ask user to enter string 1 and string 2.
3. Perform various functions such as strlen, strcmp, strcpy etc.
4. Print the following results .
5. Stop.
FLOWCHART -
START
Ask user to
enter string
1 and string
2.
Print the
following STOP
results .
PROGRAM CODE :-
//Write a program to illustrate various strings inbuilt functions (strrev, strcmp, strlen,
strcpy, strcat…)
#include<stdio.h>
#include<string.h>
int main()
{
int a,b;
char str1[100];
char str2[100];
printf("\n Enter string 1 = ");
gets(str1);
printf("\n Enter string 2 = ");
gets(str2);
while(a)
{
printf("\n Enter 1 for String length \n Enter 2 to copy the string \n Enter 3 for
string comparison \n Enter 4 for string concatenation \n Enter 5 for reversing the
string \n Enter 6 to Exit \n ");
scanf("%d", &a);
switch(a)
{
case 1:
printf("\n The length of string is = %d \n ", strlen(str1));
break;
case 2:
printf("\n The copied string is = %s \n ",strcpy(str1,str2));
break;
case 3:
b=strcmp(str1,str2);
if(b==0)
{
printf("\n Both strings are same.\n ");
}
else
{
printf("\n Both strings are different.\n ");
}
break;
case 4:
strcat(str1,str2);
printf("\n The concatenated string is = %s \n ", str1);
break;
SUBJECT NAME-Fundamentals of Computer Programming SUBJECT CODE-21CSH101
3
case 5:
printf("\n The reversed string is = %s \n ", strrev(str1));
break;
case 6:
return 0;
ERRORS OCCURRED – Didn’t used case 6 to exit due to which program was not ending.
PROGRAM’S EXPLANATION (in brief) - This program is a simple C language program used to make a
program using strings.
3] Take the max length of string and enter the program’s input as well as the variables.
4] To retrieve the output from the arithmetic method, select the print option.
OUTPUT :-
LEARNING OUTCOMES : –
Helps to to build and compile a simple C language program to use various functions using
strings.
PRACTICAL – 2.3.2
AIM OF THE EXPERIMENT – Write user defined functions for all the inbuilt functions of the above
Program.
ALGORITHM – 1. Start.
2. Ask user to enter string 1 and string 2.
3. Perform the various functions such as finding string length, compare strings,
reverse strings etc.
4. Print the results.
5. Stop.
FLOWCHART -
START
Ask user to
enter string 1
and string 2.
Print the
results.
STOP
PROGRAM CODE :–
//Write user defined functions for all the inbuilt functions of the above Program.
#include<string.h>
#include<stdio.h>
int main()
{
int a, b, i, j;
char str1[100];
char str2[100];
char str3[100];
printf("\n Enter string 1 = ");
gets(str1);
printf("\n Enter string 2 = ");
gets(str2);
while(1)
{
printf("\n Enter 1 for String length \n Enter 2 to copy the string \n Enter 3 for
string comparison \n Enter 4 for string concatenation \n Enter 5 for reversing
the string \n Enter 6 to Exit \n ");
scanf("%d", &a);
switch(a)
{
case 1:
i=0;
while(str1[i]!='\0')
{
i++;
}
printf("\n The length of string is = %d \n ", i);
break;
case 2:
i=0;
while(str1[i]!='\0')
{ str3[i]=str1[i];
i++;
}
str3[i+1]!='\0';
printf("\n The copied string is = %s \n ", str3);
break;
case 3: i=0;b=0;
while(str1[i]!='\0'||str2[i]!='\0')
{
if(str2[i]!=str1[i])
{
SUBJECT NAME-Fundamentals of Computer Programming SUBJECT CODE-21CSH101
8
b=1;
break;
}
i++;
}
if(b!=1)
printf("\n Both strings are same.\n ");
else
printf("\n Both strings are different.\n ");
break;
case 4: i=0;j=0;
while(str1[i]!='\0')
{
i++;
}
while(str2[j]!='\0')
{
str1[i+j]=str2[j];
j++;
}
str1[i+j]='\0';
printf("\n The concatenated string is = %s \n ", str1);
break;
case 5: i=0;j=0;
while(str1[i]!='\0')
{
i++;
}
printf("\n The reversed string is = \n ");
for(j=i-1;j>=0;j--)
printf("%c",str1[j]);
break;
case 6:
return 0;
default : printf("\n You have entered incorrect option.");
}
}
}
PROGRAM’S EXPLANATION (in brief) - This program is a simple C language program, used for
Finding sum, difference, product and transpose of matrices . It intends to make the calculations
faster and easier for all individuals.
3] Take the matrices and write down the program’s input as well. As well as the variables.
4] To retrieve the output from the arithmetic method, select the print option.
RESULT –
LEARNING OUTCOMES –
Helps to to build and compile a simple C language program to use various functions using
strings.