Search Your Topic

config
Wednesday, 21 June 2017 02:56

C++ Strings

Written by
Rate this item
(0 votes)

In C++, string is an object of std::string class that represents sequence of characters. We can perform many operations on strings such as concatenation, comparison, conversion etc.


C++ String Example

Let's see the simple example of C++ string.

 
  1. #include <iostream>  
  2. using namespace std;  
  3. int main( ) {  
  4.     string s1 = "Hello";    
  5.         char ch[] = { 'C''+''+'};    
  6.         string s2 = string(ch);    
  7.         cout<<s1<<endl;    
  8.         cout<<s2<<endl;    
  9. }  

Output:

Hello
C++

C++ String Compare Example

Let's see the simple example of string comparison using strcmp() function.

 
  1. #include <iostream>  
  2. #include <cstring>  
  3. using namespace std;  
  4. int main ()  
  5. {  
  6.   char key[] = "mango";  
  7.   char buffer[50];  
  8.   do {  
  9.      cout<<"What is my favourite fruit? ";  
  10.      cin>>buffer;  
  11.   } while (strcmp (key,buffer) != 0);  
  12.  cout<<"Answer is correct!!"<<endl;  
  13.   return 0;  
  14. }  

Output:

What is my favourite fruit? apple
What is my favourite fruit? banana
What is my favourite fruit? mango
Answer is correct!!

C++ String Concat Example

Let's see the simple example of string concatenation using strcat() function.

 
  1. #include <iostream>  
  2. #include <cstring>  
  3. using namespace std;  
  4. int main()  
  5. {  
  6.     char key[25], buffer[25];  
  7.     cout << "Enter the key string: ";  
  8.     cin.getline(key, 25);  
  9.     cout << "Enter the buffer string: ";  
  10.      cin.getline(buffer, 25);  
  11.     strcat(key, buffer);   
  12.     cout << "Key = " << key << endl;  
  13.     cout << "Buffer = " << buffer<<endl;  
  14.     return 0;  
  15. }  

Output:

Enter the key string: Welcome to
Enter the buffer string:  C++ Programming.
Key = Welcome to C++ Programming.
Buffer =  C++ Programming.

C++ String Copy Example

Let's see the simple example of copy the string using strcpy() function.

 
  1. #include <iostream>  
  2. #include <cstring>  
  3. using namespace std;  
  4. int main()  
  5. {  
  6.     char key[25], buffer[25];  
  7.     cout << "Enter the key string: ";  
  8.     cin.getline(key, 25);  
  9.     strcpy(buffer, key);  
  10.     cout << "Key = "<< key << endl;  
  11.     cout << "Buffer = "<< buffer<<endl;  
  12.     return 0;  
  13. }  

Output:

Enter the key string: C++ Tutorial
Key = C++ Tutorial
Buffer = C++ Tutorial

C++ String Length Example

Let's see the simple example of finding the string length using strlen() function.

 
  1. #include <iostream>  
  2. #include <cstring>  
  3. using namespace std;  
  4. int main()  
  5. {  
  6.     char ary[] = "Welcome to C++ Programming";  
  7.     cout << "Length of String = " << strlen(ary)<<endl;  
  8.     return 0;  
  9. }  

Output:

Length of String = 26
Read 144 times
Anish Sir

"I am delighted once again to pen the welcome note to the Tosh!Yas Technologies ."

 Call +91 74 88 34 7779  | Email : anishsingh@live.com

toshiyas.in

Leave a comment

Make sure you enter all the required information, indicated by an asterisk (*). HTML code is not allowed.