0% found this document useful (0 votes)
20 views21 pages

Strings

The document discusses various string handling functions in C including strcpy(), strcat(), strcmp(), strcmpi(), strrev(), strupr(), strlwr(), and strlen(). It provides the syntax and examples of using each function to copy, concatenate, compare, reverse, change case, and get the length of strings.

Uploaded by

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

Strings

The document discusses various string handling functions in C including strcpy(), strcat(), strcmp(), strcmpi(), strrev(), strupr(), strlwr(), and strlen(). It provides the syntax and examples of using each function to copy, concatenate, compare, reverse, change case, and get the length of strings.

Uploaded by

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

20CSE102

C PROGRAMMING AND
DATA STRUCTURES
UNIT III
STRINGS AND POINTERS

Presented by
Mr.J.Viswanath,ME.,
AP-AI&DS
String Handling Functions

strcpy() strlwr()
strcat() strcmp()
strlen()strcmpi()
Strupr() strrev()
Strcpy() function :
It copies the contents of one string into another string.
Syntax : strcpy(string1,string2);

#include<stdio.h>
#include<string.h>
void main()
{
char str[25],cpy[25];
printf("\n Enter a String");
gets(str);
strcpy(CPY,STR);
printf("\n The source string is %s",str);
printf("\n The copied string is %s",cpy);
}

OUTPUT Enter a String : CSC


The Source string is : CSC
The Copied string is : CSC
Strcat() function :
it concatenates the source string at the end of the target
string
Syntax : strcat(string1,string2);
#include<stdio.h>
#include<string.h>
void main()
{
char str[25],str1[25];
printf("\n Enter a String");
gets(str);
printf("\n Enter another String");
gets(str1);
printf("\n The concatenated string is %s",strcat(str,str1));
}

OUTPUT Enter a String : CSC


Enter another String : COMPUTER
The Concatenated string is : CSC COMPUTER
Strcmp() function :
It compares two strings to find whether the strings are equal or not.
Syntax : strcmp(string1,string2);

#include<stdio.h>
#include<string.h>
void main() Return values
{ 0  Equal
char str[25],str1[25]; 1  string1>string2
int x; -1  string1<string2
printf("\n Enter a String");
gets(str);
printf("\n Enter another String");
gets(str1);
x=strcmp(str,str1);
If(x==0)
printf(“\n Strings are equal”);
else if(x>0)
printf("\n The string1 %s is greater than string2 %s”,str,str1);
else
printf("\n The string2 %s is greater than string1 %s”,str1,str);
}
Enter a String : JAIKUMAR
OUTPUT Enter another String : SASIKUMAR
The string2 SASIKUMAR is greater than string1 JAIKUMAR
Strcmpi() function :
It compares two strings without regard to case to find whether the strings are
equal or not. ( i  ignorecase)
Syntax : strcmpi(string1,string2);
#include<stdio.h>
#include<string.h>
void main()
{
char str[25],str1[25];
int x;
printf("\n Enter a String");
gets(str);
printf("\n Enter another String");
gets(str1);
X=strcmpi(str,str1);
if(x==0)
printf(“\n The two Strings are equal”);
else if(x>0)
printf("\n The string1 %s is greater than string2 %s”,str,str1);
else
printf("\n The string2 %s is greater than string1 %s”,str1,str);
}
Enter a String : JAIKUMAR
OUTPUT Enter another String : jaikumar
The Two strings are equal
strrev() function :
used to reverse a string. It takes only one argument.

Syntax : strrev(string);

#include<stdio.h>
#include<string.h>
void main()
{
char str[25];
printf("\n Enter a String");
gets(str);
printf("\n The Reversed string is %s",strrev(str));
}
OUTPUT Enter a String : SHIVA
The reversed string is : AVIHS
strupr() function :
used to convert a string to uppercase. It takes only one argument.

Syntax : strupr(string);

#include<stdio.h>
#include<string.h>
void main()
{
char str[25];
printf("\n Enter a String");
gets(str);
printf("\n The case changed string is %s",strupr(str));
}

OUTPUT Enter a String : csc


The case changed string is : CSC
strlwr() function :
used to convert a string to Lowercase. It takes only one argument.

Syntax : strlwr(string);

#include<stdio.h>
#include<string.h>
void main()
{
char str[25];
printf("\n Enter a String");
gets(str);
printf("\n The case changed string is %s",strlwr(str));
}

OUTPUT Enter a String : CSC


The case changed string is : csc
strlen() function :
used to count the number of characters present in a string. It takes only
one argument.

Syntax : int variablename=strlen(string);

#include<stdio.h>
#include<string.h>
void main()
{
char str[25];
printf("\n Enter a String");
gets(str);
printf("\n The length of the string is %d",strlen(str));
}

OUTPUT Enter a String : CSC


The length of the string is : 3
Thank you

You might also like