0% found this document useful (0 votes)
15 views2 pages

CPR Experiment 8

Uploaded by

Backup
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views2 pages

CPR Experiment 8

Uploaded by

Backup
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

CPR Experiment 8:

Aim: a) Program to demonstrate String


b) Program to demonstrate String arrays of string 02
Theory:

The C String is stored as an array of characters. The difference between a character array and a C
string is that the string in C is terminated with a unique character ‘\0’. String can be declared as
char array.

Eg char name[20];

Input to String can be taken with scanf(“%s”) or gets() function.

String functions:

1) strlen() : finds length of the string


2) strcat(str1,str2) : adds the contents of second string at the end of the first string.
3) strcmp(str1,str2) : compares str1 with str2 and returns difference between the first non-matching
character pair.
4) strcpy(str1,str2) : copies second string into first string.
5. strrev(Str) : reverses the given string and stores in the same.
Exercise:

1) Write a program to input two strings and find if they are equal or not.

#include <stdio.h>
#include<string.h>
void main()
{
char str1[10],str2[10];
int d;
printf("Enter first string :");
scanf("%s",str1);
printf("Enter second string :");
scanf("%s",str2);
d=strcmp(str1,str2);
if(d==0)
printf("both strings are equal");
else
printf("Strings are not equal");
}
2) Write a program to input a name and check if the name contains less than or greater than
or equal to 6 characters. Eg : Input : Enter your name: VPMS Output : Name contains less
than 6 characters.

#include <stdio.h>
#include<string.h>
void main()
{
char name[10];
int l;
printf("Enter any name :");
scanf("%s",name);
l=strlen(name);
if(l>6)
printf("name contains more than 6 chars.");
else if(l<6)
printf("name contains less than 6 chars.");
else
printf("name contains exact 6 chars.");
}

Practical Related Theory Questions:

1. Write the syntax and use of strcpy() and strcat() string functions.

You might also like