Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
10 views

String Handling Functions

The document discusses string handling functions in C, highlighting their importance in simplifying string manipulation. It covers popular functions such as strcat(), strcmp(), strcpy(), and strlen(), providing syntax and examples for each. Additionally, it includes a task to write a program for reversing a collection of strings stored in a 2D array.

Uploaded by

Alessandro
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

String Handling Functions

The document discusses string handling functions in C, highlighting their importance in simplifying string manipulation. It covers popular functions such as strcat(), strcmp(), strcpy(), and strlen(), providing syntax and examples for each. Additionally, it includes a task to write a program for reversing a collection of strings stored in a 2D array.

Uploaded by

Alessandro
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Week 11

String Handling functions

String Handling Functions

• We need to often manipulate strings according to the need of a problem.

• Most, if not all, of the time string manipulation can be done manually but, this makes programming
complex and large.

• To solve this, C supports a large number of string handling functions in the standard library
"string.h".

String Handling Functions

• Following are the popular String handling functions available in string.h header file

strcat()

strcmp()

strcpy()

strlen()

String Handling Functions


String Concatenation

• It appends a copy of source string to the end of destination string.

• length of the resulting string is sum of the length of source and destination string

• General Syntax

strcat(dest, src);

• In this, the dest should be a character array variable and src can either be a string constant or a
char array.

#include <stdio.h>

#include <string.h>

int main(){

char str1[20]="Programming";
char str2[20]="Logic";

strcat(str1,str2);

printf("Course name : %s\n",str1);

return 0;

Output : ProgrammingLogic

String Comparison

• It performs an comparison of two strings, with case sensitivity.

• General Syntax

strcmp(string1, string2);

• comparison starts with the first character in each string and continues with subsequent
characters until the corresponding characters differ or until the end of the strings is reached.

String Comparison

• It returns an integer value that is:

less than 0 if first string is lesser than second

equal to 0 if both strings are equal

greater than 0 if first string is greater than second

#include <stdio.h>

#include <string.h>

int main(){

char str1[20]="ABC"; char str2[20]="XYZ";

if (strcmp(str1,str2) < 0)

printf("%s is lesser than %s",str1,str2);

else if (strcmp(str1,str2) > 0)

printf("%s is greater than %s",str1,str2);

else

printf("Both Strings are equal");

return 0;
}

Output: ABC is lesser than XYZ

String Copy

• Copies the source String to destination string

• Syntax

strcpy(dest, src);

• Example

char course[40];

strcpy(course, “C Programming”);

• Now course will get the value “C

Programming”.

• The strcpy() function also returns the copied string.

String Length

• It is used to calculate the number of

characters in the string (excluding NULL char).

• Syntax

strlen(string);

• It returns length of the string(integer).

#include <stdio.h>

#include <string.h>

int main(){

char str1[20]="C Programming";

int length;

length = strlen(str1);

printf("Length of the given String : %d",length);


return 0;

Output : Length of the given String : 13

Write a program to reverse a collection of strings stored in an 2D array.

You might also like