0% found this document useful (0 votes)
169 views

C++ Strings: C++ String Compare Example

The document contains examples of performing various operations on C++ strings such as comparison, concatenation, copying, and finding length. It demonstrates using functions like strcmp(), strcat(), strcpy(), and strlen() to manipulate and work with C++ string objects and character arrays. The examples compile and run as expected, outputting the results of each string operation.

Uploaded by

Khisrav
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
169 views

C++ Strings: C++ String Compare Example

The document contains examples of performing various operations on C++ strings such as comparison, concatenation, copying, and finding length. It demonstrates using functions like strcmp(), strcat(), strcpy(), and strlen() to manipulate and work with C++ string objects and character arrays. The examples compile and run as expected, outputting the results of each string operation.

Uploaded by

Khisrav
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

C++ Strings C++ String Compare Example

In C++, string is an object of std::string class that represents sequence of Let's see the simple example of string comparison using strcmp() function.
characters. We can perform many operations on strings such as
concatenation, comparison, conversion etc. 1. #include <iostream>  
2. #include <cstring>  
3. using namespace std;  
4. int main ()  
5. {  

C++ String Example 6.


7.
  char key[] = "mango";  
  char buffer[50];  
8.   do {  
Let's see the simple example of C++ string. 9.      cout<<"What is my favourite fruit? ";  
10.      cin>>buffer;  
1. #include <iostream>   11.   } while (strcmp (key,buffer) != 0);  
2. using namespace std;   12.  cout<<"Answer is correct!!"<<endl;  
3. int main( ) {   13.   return 0;  
4.     string s1 = "Hello";     14. }  
5.         char ch[] = { 'C', '+', '+'};     1. #include <iostream>  
6.         string s2 = string(ch);     2. #include <cstring>  
7.         cout<<s1<<endl;     3. using namespace std;  
8.         cout<<s2<<endl;     4. int main ()  
9. }   5. {  
1. #include <iostream>   6.   char key[] = "mango";  
2. using namespace std;   7.   char buffer[50];  
3. int main( ) {   8.   do {  
4.     string s1 = "Hello";     9.      cout<<"What is my favourite fruit? ";  
5.         char ch[] = { 'C', '+', '+'};     10.      cin>>buffer;  
6.         string s2 = string(ch);     11.   } while (strcmp (key,buffer) != 0);  
7.         cout<<s1<<endl;     12.  cout<<"Answer is correct!!"<<endl;  
8.         cout<<s2<<endl;     13.   return 0;  
9. }   14. }  

Output: Output:

Hello What is my favourite fruit? apple


C++ What is my favourite fruit? banana
What is my favourite fruit? mango
Answer is correct!!
C++ String Concat Example C++ String Copy Example
Let's see the simple example of string concatenation using strcat() function. Let's see the simple example of copy the string using strcpy() function.

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

Enter the key string: Welcome to


Enter the buffer string: C++ Programming.
Key = Welcome to C++ Programming.
Buffer = C++ Programming.
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. }  
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

You might also like