A string is a sequence of characters.

  • In C++, strings can be represented in two ways:

    1. C-style strings (character arrays)

    2. String class (from <string> library)

Strings are used to store words, sentences, or any textual data.

C-Style Strings

  • These are arrays of characters ending with a null character \0.

  • Null character \0 indicates the end of the string.

Declaration Syntax:

 
char str[20]; // array of 20 characters

Initialization Examples:

 
char name[10] = "Ali"; // automatically adds '\0'

char name2[10] = {'A','l','i','\0'}; // manual null termination

Important Notes:

  • Maximum length = size of array – 1 (because of \0)

  • Cannot directly assign new strings using = after declaration.

Input and Output

Printing C-style Strings:

 
#include <iostream>
using namespace std;
int main() {
char name[] = "Muhammad";
cout << name; // Output: Muhammad
return 0;
}

Input C-style Strings:

 
#include <iostream>

using namespace std;
int main() {
char name[20];
cout << "Enter your name: ";
cin >> name; // reads until space

cout << "Hello, " << name;
return 0;
}

Limitation:

  • cin reads input only until space.

  • To read full sentence, use cin.getline():

 
cin.getline(name, 20);

C-Style String Functions

C++ provides string handling functions in <cstring> library.

  1. strlen() – Length of string

 
#include <cstring>

char str[] = "Hello";
cout << strlen(str); // Output: 5
  1. strcpy() – Copy string

 
char str1[10];
char str2[] = "Ali";
strcpy(str1, str2);

cout << str1; // Output: Ali
  1. strcat() – Concatenate strings

 
char str1[20] = "Hello ";
char str2[] = "World";
strcat(str1, str2);
cout << str1; // Output: Hello World
  1. strcmp() – Compare strings

 
char str1[] = "Ali";
char str2[] = "Ahmed";
if(strcmp(str1, str2) == 0)
cout << "Equal";
else cout << "Not equal"; // Output: Not equal
  1. strchr() – Find character in string

 
char str[] = "Hello";
char *ptr = strchr(str, 'e');
cout << ptr; // Output: ello
  1. strstr() – Find substring

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

Limitations of C-Style Strings

  • Fixed size; cannot grow dynamically

  • Manual null character required

  • Harder to manipulate

  • Functions are from C library, not fully type-safe

String Class in C++

C++ provides string class in <string> library.

Declaration:

 
#include <iostream>
#include <string>
using namespace std;
int main() {
string name = "Muhammad";
cout << name;
return 0;
}

Advantages over C-style strings:

  • Dynamic size; grows automatically

  • Supports operators like + for concatenation

  • Easier functions to manipulate strings

  • No need for null character \0

String Input and Output

Using cin and cout:

 
string name;
cin >> name; // reads until space

cout << "Hello " << name;

Using getline() for full sentence:

 
string sentence;
getline(cin, sentence);
cout << sentence;

String Functions and Operators

  1. Concatenation:

 
string s1 = "Hello";
string s2 = "World";
string s3 = s1 + " " + s2;
cout << s3; // Output: Hello World
  1. Length of String:

 
string s = "Hello";
cout << s.length(); // Output: 5
  1. Access Character:

 
cout << s[1]; // Output: e
  1. Substring:

 
string s = "Hello World";
cout << s.substr(0,5); // Output: Hello
  1. Find:

 
string s = "Hello World";
cout << s.find("World"); // Output: 6 (starting index)
  1. Replace:

 
string s = "Hello World";
s.replace(6, 5, "Ali");
cout << s; // Output: Hello Ali
  1. Erase:

 
string s = "Hello World";
s.erase(5,6);
cout << s; // Output: Hello
  1. Compare:

 
string s1 = "abc";
string s2 = "xyz";
cout << s1.compare(s2); // negative value since s1 < s2

Examples Using String Class

  1. Concatenate two strings

  2. Find the length of string

  3. Input full name using getline()

  4. Convert string to uppercase (using loop)

  5. Count vowels in string

  6. Reverse a string

  7. Check palindrome string

  8. Replace a word in a sentence

  9. Find substring index

  10. Compare two strings

Example 1: Count Vowels

 
string s = "Hello World";
int count=0;
for(char c : s){
if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u'||c=='A'||c=='E'||c=='I'||c=='O'||c=='U')
count++;
}
cout << "Vowels = " << count; // Output: Vowels = 3

Example 2: Reverse String

 
string s = "Hello";
reverse(s.begin(), s.end());
cout << s; // Output: olleH

Example 3: Check Palindrome

 
string s = "madam";
string rev = s;
reverse(rev.begin(), rev.end());
cout << (s == rev ? "Palindrome" : "Not Palindrome"); // Output: Palindrome

Example 4: Replace Word

 
string s = "Hello World";
s.replace(6, 5, "Ali");
cout << s; // Output: Hello Ali

Example 5: Substring

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

Example 6: Find Word Position

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

Example 7: Concatenate Strings

 
string s1 = "Good";
string s2 = "Morning";
cout << s1 + " " + s2; // Output: Good Morning

Example 8: Compare Strings

 
string s1 = "abc";
string s2 = "xyz";
cout << s1.compare(s2); // negative (s1 < s2)

Example 9: Erase Part of String

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

Example 10: Convert to Uppercase

 
string s = "hello";
for(char &c : s)
c = toupper(c);
cout << s; // Output: HELLO

Extra Details

  • C-style strings are low-level, faster, but harder to use

  • String class is easier, safer, and dynamic

  • Use string class in modern C++ unless performance is critical

  • Can combine string class and C-style strings if needed

  • Strings can be passed to functions, returned from functions