0% found this document useful (0 votes)
335 views20 pages

PPS Unit-3 Strings

The document discusses various string handling functions in C programming like strlen(), strcpy(), strcat(), strcmp(), strlwr(), strupr(), and strrev(). It explains what each function does, provides syntax examples, and illustrates sample code to demonstrate how to use the string functions to perform operations like getting the length of a string, copying strings, concatenating strings, comparing strings, converting case of strings, and reversing strings. The document is a useful reference for learning how to manipulate and work with string data in C using the standard library string handling functions.

Uploaded by

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

PPS Unit-3 Strings

The document discusses various string handling functions in C programming like strlen(), strcpy(), strcat(), strcmp(), strlwr(), strupr(), and strrev(). It explains what each function does, provides syntax examples, and illustrates sample code to demonstrate how to use the string functions to perform operations like getting the length of a string, copying strings, concatenating strings, comparing strings, converting case of strings, and reversing strings. The document is a useful reference for learning how to manipulate and work with string data in C using the standard library string handling functions.

Uploaded by

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

II

g functions
PRESENTED BY
M.YUGANDHAR
CSE DEPARTMENT
AITAM
String Handling
• String is an array of characters(A string is actually a on dimensional array of characters).

• String is a sequence of characters that are treated as a single data item and terminated by a null
character '\0'. 
String Declaration and initialization:
• Method 1: char arrayname[ ]={'H', 'E', 'L', 'L', 'O', '\0'};
• Method 2: char arrayname[ ]="HELLO";

• In the above declaration NULL character (\0) will automatically be inserted at the end of the string.
• What is NULL Char “\0”?
'\0' represents the end of the string. It is also referred as String terminator & Null Character.

 
String Handling
#include <stdio.h>
char a[] = "Hello" void main ()
{

char a[6] = {'H', 'e', 'l', 'l', 'o', '\0'};


printf(" %s", a);

Note: %s format specifier is used for strings input/output


gets() and puts()
gets() and puts()
• Functions gets() and puts() are two string functions to take string input from the
user and display it respectively.

Note:
• Though, gets() and puts() function handle strings, both these functions are defined
in "stdio.h" header file.

• C supports a large number of string handling functions in the standard library


"string.h".
String I/O in C programming
Read & write Strings in C using Printf() Read & Write Strings in C using gets() and
and Scanf() functions puts() functions
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
void main() void main()
{ {
char a[20]; char a[20];
printf("Enter your name: "); puts("Enter your name:");
scanf("%s", a); gets(a);
printf("%s", a); puts(a);
} }

Output: Output:
Enter your name: Sanket Enter your name: Sanket
Sanket Sanket
commonly used string handling functions

Function Work of Function


strlen( ) computes string's length
strcpy( ) copies a string to another
strcat( ) concatenates(joins) two strings
strcmp( ) compares two strings
strlwr( ) converts string to lowercase
strupr( ) converts string to uppercase
1. strlen()
• It is used to computes string's length

• strlen returns you the length of the string stored in array.

• However sizeof returns the total allocated size assigned to the array.

Syntax:
Int var.name = strlen(string or arrayname)
Example of strlen():

#include <stdio.h>
#include <string.h>
void main()
{
char str1[20] = "vaishnavtej";
int a=strlen(str1);
printf("Length of string str1: %d", a);
}

Output:
Length of string str1: 11
2.strcpy()
• It copies the string str2 into string str1, including the end character (terminator char
‘\0’).

Syntax:
strcpy(destination,source)

Or

strcpy(str1,str2)
Example of strcpy()
#include <stdio.h>
#include <string.h>
void main()
{
char s1[30] = "sanket";
char s2[30] = "sohel";
/* this function has copied s2 into s1*/
strcpy(s1,s2);
printf("s1 = %s", s1);
printf("s2 = %s", s2);
}
3.stringlwr() Syntax:
•It will convert string into lowercase strlwr(string/arrayname )

#include<stdio.h>
#include<string.h>
void main()
{
char str[ ] = "MAKE YOUR DREAMS COME TRUE";
printf(“Lowercase: %s", strlwr(str) );
}
Output:
Lowercase: make your dreams come true
4.stringupr() Syntax:
•It will convert string into uppercase strupr(string /arrayname)

#include<stdio.h>
#include<string.h>
void main()
{
char str[ ] = “dreams don’t work unless you take action";
printf("Uppercase: %s", strupr(str) );
}
Output:
Uppercase: DREAMS DON’T WORK UNLESS YOU TAKE ACTION
5.strcat()
• It concatenates two strings and returns the concatenated string.

• Right side string will be added to at the end of the left side string.

• It concatenates n characters of str2 to string str1. A terminator char (‘\0’) will


always be appended at the end of the concatenated string.

Syntax:
strcat(string1,string2)
5.strcat()
#include<stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
Void main() void main()
{ {
char s1[30] = “Sanket"; char s1[10] = "Sanket";
char s2[10] = “Sahadev"; char s2[10] = "Sahadev";
strcat(s1,s2); strncat(s1,s2, 4); //it take first 3
printf("after concatenation: %s",s1); characters from second string to
} first string
printf("Concatenation : %s", s1);
Output: after concatenation: SanketSahadev }

Output: Concatenation :
SanketSaha
6. strcmp ()
• It compares the two strings and returns an integer value.
• If string1 < string2 then it would result in a negative value.
• If string1 > string2 then it would return positive value.
• If string1 == string2 then you would get 0(zero) when you use this function for
compare strings.

Syntax:
strcmp(string1,string2 )
6. strcmp ()
#include <stdio.h>
#include <string.h>
void main()
{
char s1[20] = “Geetham";
char s2[20] = “geetham";
if (strcmp(s1, s2) ==0)
{
printf("string 1 and string 2 are equal");
}
else
{
printf("string 1 and 2 are different
}
}
Output: string 1 and 2 are different
7. strrev()
• This function is used to reverses a given string.

Syntax:
strrev(string/arrayname );
7. strrev()
#include<stdio.h>
#include<string.h>
void main()
{
char a[30] = “hello”;

printf(“String before reverse : %s\n”, a);

printf(“String after reverse : %s ”, strrev(a) );


}

Output: String before reverse : hello


String after reverse : olleh
Other String functions

You might also like