You are on page 1of 13

EXPERIMENT 2.

NAME- SAGAR
UID-21BCS3490
CLASS/GROUP-4 b
SEMESTER- 1
Subject-FUNDAMENTAL OF COMPUTER PROGRAMMING

AIM

LEARN HOW TO PERFORM STRING OPERATIONS USING C

PREREQUISITES-
BASIC KNOWLEDGE OF STRINGS

LIST OF SUB PROGRAMS-


Program 6.1: Write a program to illustrate various strings inbuilt functions

(strrev, strcmp, strlen, strcpy, strcat…)

 Flowchart/ Algorithm:
 Program Code:
#include <string.h>
#include <stdio.h>
int main(){
char a[30], b[30];
int c,d;
printf("Enter string 1:\n");
gets(a);
printf("Enter string 2:\n");
gets(b);
start:
printf("\n1. String length\n2. String Copy\n3. String Comparison\n4. String Concatenation\n5.
Reverse\n6. Exit\nEnter the operation you wish to perform (1-6)\n");
scanf("%d",&c);
switch(c){
case 1:
printf("Length of string 1 is %d\n",strlen(a));
printf("Length of string 2 is %d\n",strlen(b));
goto start;
case 2:
strcpy(a,b);
printf("Copied string is %s\n",a);
goto start;
case 3:
d=strcmp(a,b);
if(d==0){
printf("The strings are same\n");
}
else{
printf("The strings are different\n");
}
goto start;
case 4:
strcat(a,b);
printf("Concatenated string is %s\n",a);
goto start;
case 5:
printf("Reverse of string 1 is
%s\n",strrev(a)); printf("Reverse of string 2 is
%s\n",strrev(b)); goto start;
case 6:
goto end;
default:
printf("You entered invalid option\n");
goto start;
}
end:
return 0;
}

 Errors Encountered During Program’s Execution:


No error encountered during program’s execution.

 Program’s Explanation:
Program that takes two strings from the user and ask user for which operation of
string to be performed on given strings and after getting option from user program
will perform that operation and give desired output to the user.

 Output:
Program 6.2: Write user defined functions for all the inbuilt functions of the
above Program.

 Flowchart/ Algorithm:
strlen:
strcpy:

strcmp:

strcat:
strrev:
 Program Code:
#include <string.h> #include <stdio.h> int main(){
char a[30], b[30]; printf("Enter string 1:\n"); gets(a);
printf("Enter string 2:\n"); gets(b);
start:
printf("\n1. String length\n2. String Copy\n3. String Comparison\n4. String Concatenation\n5.
Reverse\n6. Exit\nEnter the operation you wish to perform (1-6)\n"); char g[30],h[30];
int c,n=0,d=0,i,j,l=0,e=0,f=0; scanf("%d",&c);
switch(c){
case 1:
for(i=0;a[i]!='\0';i++){ n++;
}
for(j=0;b[j]!='\0';j++){ l++;
}
printf("Length of string 1 is %d\n",n); printf("Length of string 2 is %d\n",l); goto start;
case 2:
for(i=0;a[i]!='\0';i++){
a[i]=b[i];
}
printf("Copied string is %s\n",a);
goto start;
case 3:
for(i=0;a[i]!='\0';i++){
e++;
if(a[i]==b[i]){
d+=1;
}
}
if(e==d){
printf("The strings are same\n");
}
else{
printf("The strings are different\n");
}
goto start;
case 4:
for(i=0;a[i]!='\0';i++){
n++;
}
for(j=0;b[j]!='\0';j++){
l++;
}
for(i=n; i<n+l; i++ ){
a[i]=b[f];
f++;
}
a[i]='\0';
printf("Concatenated string is %s\n",a);
goto start;
case 5:
for(i=0;a[i]!='\0';i++){
g[i]=a[i];
n++;
}
for(j=0;b[j]!='\0';j++){
h[j]=b[j];
l++;
}
for(i=0;a[i]!='\0';i++)
{ a[i]=g[n-1];
n--;
}
for(j=0;b[j]!='\0';j++){
b[j]=h[l-1];
l--;
}
printf("Reverse of string 1 is %s\n",a); printf("Reverse of
string 2 is %s\n",b); goto start;
case 6:
goto end; default:
printf("You entered invalid option\n"); goto start;
}
end: return 0;

 Errors Encountered During Program’s Execution:


When making user defined function for reverse of string function error encountered
during program’s execution. After checking the function again and problem get
resolved.

 Program’s Explanation:
Program that takes two strings from the user and ask user for which operation of
string to be performed on given strings and after getting option from user program
will perform that operation and give desired output to the user. This program is
same of previous program just strings inbuilt functions are defined by user in this
program.

 Output:
LEARNING OUTCOMES-
1. I have learned about strings in C programming language.
2. I have learned about strings inbuilt functions in C programming language.
3. I have learned how to make user defined functions of string inbuilt functions in C
programming language.
4. I have learned about use of ‘\0’ in string. It used to indicate the termination of a
character string.
5. I have learned different inbuilt string functions other than these inbuilt functions
which are included in these programs.
EVALUATION COLUMN-
Sr. Parameters Maximum Marks
No. Marks Obtained
1. Worksheet Completion including writing 10
learning objective/ Outcome
2. Post-Lab Quiz Result 5

3. Student engagement in Simulation/ 5


Performance/ Pre-Lab Questions
4. Total Marks 20

You might also like