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

Unit - 2 - String Handling Functions(01.07.2021)

The document provides an overview of string handling functions in C, which are defined in the string.h header file. It includes examples of commonly used functions such as strcpy(), strlen(), strcmp(), strcat(), and strrev(), along with their syntax and output. Each function is explained with sample code demonstrating its usage in C programming.

Uploaded by

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

Unit - 2 - String Handling Functions(01.07.2021)

The document provides an overview of string handling functions in C, which are defined in the string.h header file. It includes examples of commonly used functions such as strcpy(), strlen(), strcmp(), strcat(), and strrev(), along with their syntax and output. Each function is explained with sample code demonstrating its usage in C programming.

Uploaded by

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

http://www.btechsmartclass.com/c_programming/C-String-Handling-Functions.

html

for examples: https://beginnersbook.com/2014/01/c-strings-string-functions/

String Handling Functions in C


(basic functions)

C programming language provides a set of pre-defined functions called string handling


functions to work with string values. The string handling functions are defined in a
header file called string.h. Whenever we want to use any string handling function we
must include the header file called string.h.
The following table provides most commonly used string handling function and their
use...

1. strcpy() - syntax - strcpy(string1, string2)

#include <stdio.h>

#include <string.h>

int main()

char s1[30] = "python";

char s2[30] = "Java";

/* this function has copied s2 into s1*/

strcpy(s1,s2);

printf("String s1 is: %s\n", s1);

printf("String s2 is: %s", s2);


return 0;

2. strlen() Syntax : strlen(string1) - return - integer

strlen returns you the length of the string stored in array,

#include <stdio.h>

#include <string.h>

int main()

char str1[20] = "Computer";

printf("Length of string str1: %d", strlen(str1));

return 0;

Output:

Length of string str1: 8

3. strcmp() – Syntax - strcmp(string1, string2) – string comparision

Returns 0 if string1 and string2 are the same;


It compares the two strings and returns an integer value. If both the strings are
same (equal) then this function would return 0 otherwise it may return a negative
or positive value based on the comparison.
#include <stdio.h>
#include <string.h>
int main()
{
char s1[20] = "BeginnersBook";
char s2[20] = "BeginnersBook.COM";
if (strcmp(s1, s2) ==0)
{
printf("string 1 and string 2 are equal");
}else
{
printf("string 1 and 2 are different");
}
return 0;
}
Output:

string 1 and 2 are different

4. strcat() - sytax - strcat(string1,string2) -Appends string2 to string1

It concatenates two strings and returns the concatenated string.

#include <stdio.h>
#include <string.h>
int main()
{
char s1[10] = "Hello";
char s2[10] = "World";
strcat(s1,s2);
printf("Output string after concatenation: %s", s1);
return 0;
}
Output:

Output string after concatenation: HelloWorld


5. strrev() - strrev(string1) - It reverses the value of string1
The strrev(string) function returns reverse of the given string. Let's see a
simple example of strrev() function.

#include<stdio.h>

#include<string.h>

int main()

char name[30] = "Hello";

printf("String before strrev( ) : %s\n",name);

printf("String after strrev( ) : %s",strrev(name));

return 0;

String before strrev( ) : Hello


String after strrev( ) : olleH

You might also like