CPR Experiment 8
CPR Experiment 8
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];
String functions:
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.");
}
1. Write the syntax and use of strcpy() and strcat() string functions.