C++ String Handling

String is a sequence of characters (including alphabet, numbers or special symbols). C++ provides a complete library called string.h / cstring for processing of string type data. This header file include many useful built in functions to process strings. These functions include string comparison, strings concatination, string case conversion.

String Functions in C++

Following are the most commonly used C++ string functions.

Sr. Function Description Code Example Output
1 strcat strcat function combine provided strings into single string. Second string is append to thefirst string. char str1[10] = "Hello";
  char str2[10] = "World";
  cout<<strcat(str1,str2);
HelloWorld
2 strncat strncat c++ function is used to append specified no of characters from second string to the end of first string. char str1[15] = "Institute";
  char str2[15] = "Management";
  cout<<strncat(str1,str2,3);
InstituteMan
3 strcmp strcmp function is used used to compare two strings base on their ASCII values. This function will return 1 if first string is greater, return -1 if seond string is greater. It will return 0 if both strings are equal.   char str1[15] = "Institue";
  char str2[15] = "Institue ";
  cout<<strcmp(str1,str2);
-1
4 strncmp strncmp C++ function compare N characters from the input strings. Similar to strcmp it will return some integer value 1,0, -1 depending upon ASCII comparison. char str1[15] = "Z Management";
  char str2[15] = "A Management";
  cout<<strncmp(str1,str2,2);
1
5 strlen C++ strlen built-in function return the total length of input string. It calculate all available characters in string including white spaces. char str1[30] = "Institute Management System";
  
  cout<<"Total Characters: "<<strlen(str1);
Total Characters: 27
6 strstr C++ strstr function is used to fincd the first occurance of provided second string into first string. This function will return the complete string from first occurance to the end of first string. char str1[30] = "Institute Management System";
  char str2[15] = "Management";
  cout<<strstr(str1,str2);
Management System
7 strchr strchr function find first occurance of specified character and print remaining string from this character. It is case sensitive for searching character from string. char str1[30] = "Institute Management System";
  
  cout<<"First Occurance of S: "<<strchr(str1,'S');
First Occurance of S: System
8 strrchr C++ strrchr string function detect the last occurance of input character from the provided string. Similar to strchr function it will also print the remaining string strat from the last occurance of character. char str1[40] = "Institute Management System Style";
  
  cout<<"Last Occurance of S: "<<strrchr(str1,'S');
Last Occurance of S: Style
9 strpbrk strpbrk string function print all characters from first string starting from the first matching charactr from second string. char str1[30] = "Institute Management System";
  char str2[15] = "Management";
  cout<<strpbrk(str1,str2);
nstitute Management System
10 strcpy strcpy C++ is string copy built-in function it copies all characters from one string to another string. It first truncate the string and then copy characters of second string. char str1[30] = "Institute Management System";
  char str2[15] = "Hello World";
  strcpy(str1,str2);
  cout<<str1;
Hello World

 

Comments
Login to TRACK of Comments.