0% found this document useful (0 votes)
5 views8 pages

C String Manipulation Functions Guide

The document outlines various string manipulation functions available in C's <string.h> library. Key functions include strcpy for copying strings, strcat for concatenating them, strlen for measuring string length, strcmp for comparing strings, strchr for finding characters, strstr for locating substrings, and strrev for reversing strings. Each function is accompanied by its purpose, syntax, and examples.

Uploaded by

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

C String Manipulation Functions Guide

The document outlines various string manipulation functions available in C's <string.h> library. Key functions include strcpy for copying strings, strcat for concatenating them, strlen for measuring string length, strcmp for comparing strings, strchr for finding characters, strstr for locating substrings, and strrev for reversing strings. Each function is accompanied by its purpose, syntax, and examples.

Uploaded by

roshansabika2007
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

String Manipulation Functions

C provides a set of built-in library functions in <string.h> to


perform operations on strings.
strcpy()
Purpose:Copy one string into another.
Syntax: strcpy(destination, source);
Example:
char str1[20], str2[20];
strcpy(str1, "Hello");
strcpy(str2, str1); // str2 now contains "Hello"

strcat()

Purpose: Concatenate (join) two strings.


Syntax: strcat(destination, source);
Example:
char str1[20] = "Hello ", str2[] = "World";
strcat(str1, str2); // str1 now contains "Hello World"

strlen()

Purpose: Find the length of a string.


Syntax: int len = strlen(string);
Example:
char str[] = "Hello";
int len = strlen(str); // len = 5

strcmp()

Purpose: Compare two strings.


Syntax: strcmp(str1, str2);
Return Value:
0 if strings are equal
Negative if str1 < str2
Positive if str1 > str2
Example:
strcmp("abc", "abd"); // returns negative value

strrev() (Non-standard, may need custom function)

Purpose: Reverse a string.


Example:
char str[] = "Hello";
// After reverse: "olleH"

strchr()

Purpose: Find the first occurrence of a character in a string.


Syntax: char ptr = strchr(str, 'a');
strstr()

Purpose: Find the first occurrence of a substring in a string.


Syntax: char ptr = strstr(str, "sub");

String Library Functions

| Function | Purpose | Example |


| -------- | ----------------------------- | ------------------------- |
| strcpy | Copy string | strcpy(str2, str1); |
| strcat | Concatenate strings | strcat(str1, str2); |
| strlen | Find length of string | len = strlen(str); |
| strcmp | Compare strings | strcmp(str1, str2); |
| strchr | Find character in string | ptr = strchr(str, 'a'); |
| strstr | Find substring in string | ptr = strstr(str, "sub"); |
| strrev | Reverse string (non-standard) | strrev(str); |

You might also like