You are on page 1of 11

1

EXPERIMENT NUMBER - 6

STUDENT’S NAME – ABHISHEK MALHAN


STUDENT’S UID – 21BAS1056
CLASS AND GROUP – 1-A
SEMESTER - 1

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.

Perform various functions


such as strlen, strcmp,
strcpy etc.

Print the
following STOP
results .

SUBJECT NAME-Fundamentals of Computer Programming SUBJECT CODE-21CSH101


2

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;

default : printf("\n You have entered incorrect option.");


}
}
}

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.

Following steps must be followed to make the program work:

1] Begin by opening a header file.

2] Write the main ( ) function, which is a must-have function.


SUBJECT NAME-Fundamentals of Computer Programming SUBJECT CODE-21CSH101
4

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 : –

 This practical introduced me to the fundamentals of C programming language.

 Understanding the ways of execution and debugging the programs in C language.

 It provides a better outlook of course.

Helps to to build and compile a simple C language program to use various functions using
strings.

SUBJECT NAME-Fundamentals of Computer Programming SUBJECT CODE-21CSH101


5

EVALUATION COLUMN (To be filled by concerned faculty only) -

Sr. No. Parameters Maximum Marks


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

SUBJECT NAME-Fundamentals of Computer Programming SUBJECT CODE-21CSH101


6

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.

Perform the various


functions such as finding
string length, compare
strings, reverse strings etc.

Print the
results.

STOP

SUBJECT NAME-Fundamentals of Computer Programming SUBJECT CODE-21CSH101


7

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.");
}
}
}

SUBJECT NAME-Fundamentals of Computer Programming SUBJECT CODE-21CSH101


9

ERROR OCCURRED :- [Error] 'j' was not declared in this scope


Error in case 2:

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.

Following steps must be followed to make the program work:

1] Begin by opening a header file.


SUBJECT NAME-Fundamentals of Computer Programming SUBJECT CODE-21CSH101
10

2] Write the main ( ) function, which is a must-have function.

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 –

 This practical introduced me to the fundamentals of C programming language.

 Understanding the ways of execution and debugging the programs in C language.

 It provides a better outlook of course.

 Helps to to build and compile a simple C language program to use various functions using
strings.

SUBJECT NAME-Fundamentals of Computer Programming SUBJECT CODE-21CSH101


11

EVALUATION COLUMN (To be filled by concerned faculty only) –

Sr. No. Parameters Maximum Marks


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

SUBJECT NAME-Fundamentals of Computer Programming SUBJECT CODE-21CSH101

You might also like