Different String Functions in C and C++

Ayushi Sheode
4 min readJan 6, 2021

Introduction :

Strings are defined as an array of characters . The difference between a character array and a string is that string is terminated with a special character ‘\0’. There are many predefined string functions which makes our task easier .

Some of the most commonly used String functions are:

In C we need to include string.h header file and

in C++ ,cstring header file.

Let us assume that string name is str1

§ strlen(str1) : This function helps to find the length of the string .

Eg:- char str1[]=“hello”; , its size would be 5 .

§ Concatenate :

strcat(destination,source); : It concatenates two strings. The strcat() function will append a copy of the source string to the end of destination string.

§ strcpy(destination,source); : It is used to copy one string to another.

destination: Pointer to the destination array where the content is to be copied.

source: string which will be copied.

char destination[ ]=“ ”; (here empty string)

char source[ ] =“hello”;

§ strchr(main,char); : To check whether the given char is present in main string.

§ strrchr(main,char); : To find ocuurence of char in the main string from right hand side (in reverse order).

§ strcmp(str1,str2); : This function takes two strings as arguments and compare these two strings lexicographically. It compares according to dictionary .

It returns values negative , positive or zero .

String Class :

We need to add the header file <string> in c++ .

Syntax :

#include<iostream.h>

#include<string>

using namespace std;

int main()

{

string str; //declare a string

cin>>str;

return 0;

}

String class functions :

Here I am taking s as the name of string.

§ s.length() ; : To find the length of the string.

§ s.size(); : similar to length function.

§ s.clear(); : it clear contents of the string.

§ s.empty(); : finds whether string is empty or not.

§ Append function : Adds new content to the string.

string s=”hello”;

s.append(“world”);

§ Insert function : It insert a given string at a given index.

string s=”hello”;

s.insert(3,”kk”);

output : helkklo

§ Replace function :

string s=”programming”;

s.replace(3,4,”kk”); (replace 4 characters from 3rd index by “kk”)

output: prokkming

§ s.erase(); : Erases all characters in a string.

§ s.push_back(‘z’); : insert a single char at end of the string.

§ s.pop_back(); : deletes a single char at end of the string.

§ S1.swap(S2); : swaps two string.

§ s.find(str) or s.find(char) : To find the occurrence of a string or a character inside the main string.

string str=”how are you”;

str.find(“are”); // will return the starting index

str.find(‘y’); //will return the index

§ s.rfind(str) s.find(char) : To find the occurrence of a string or a character inside the main string from right hand side (in reverse order).

§ s.find_first_of(‘ ’); : To find the occurrence of a character from left hand side of a string (finds first occurence) .

string s=”hello”;

s.find_first_of(‘l’); // returns index , output : 2

There are some other functions :

§ s.front(); : To find the first letter of string.

§ s.back(); : To find the last letter of the string.

§ Concatenate usong ‘+’ operator :

string s1=”hello”;

string s2=”world”;

string s3=s1+s2; // output s3=”helloworld”

§ at() function : to find the char at a particular index.

string s=”hello”;

s.at(0); // returns ‘h’

Thanks for reading !!!

--

--