0% found this document useful (0 votes)
23 views2 pages

CPP String Functions

Uploaded by

walrus131811
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)
23 views2 pages

CPP String Functions

Uploaded by

walrus131811
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
You are on page 1/ 2

C++ String and C-string Built-in Functions with Examples

std::string: length()

Description: Returns the number of characters in the string.


Example:
std::string s = "Hello";
std::cout << s.length(); // Output: 5

std::string: append()

Description: Appends a string to the current string.


Example:
std::string s = "Hello";
s.append(" World");
std::cout << s; // Output: Hello World

std::string: substr()

Description: Returns a substring starting at a given position.


Example:
std::string s = "Hello World";
std::cout << s.substr(6, 5); // Output: World

std::string: find()

Description: Finds the first occurrence of a substring.


Example:
std::string s = "Hello World";
std::cout << s.find("World"); // Output: 6

std::string: compare()

Description: Compares two strings lexicographically.


Example:
std::string s1 = "apple", s2 = "banana";
std::cout << s1.compare(s2); // Output: negative number

cstring: strlen()

Description: Returns the length of a C-string.


Example:
const char* str = "Hello";
std::cout << strlen(str); // Output: 5
C++ String and C-string Built-in Functions with Examples

cstring: strcpy()

Description: Copies one string into another.


Example:
char dest[20];
strcpy(dest, "Hello");
std::cout << dest; // Output: Hello

cstring: strcmp()

Description: Compares two strings.


Example:
std::cout << strcmp("abc", "abd"); // Output: negative number

cstring: strcat()

Description: Concatenates two strings.


Example:
char a[20] = "Hello";
char b[] = " World";
strcat(a, b);
std::cout << a; // Output: Hello World

cstring: strstr()

Description: Finds a substring in a C-string.


Example:
char str[] = "Hello World";
std::cout << strstr(str, "World"); // Output: World

You might also like