C++ Example || C++ is a popular programming language used by software developers worldwide.

It offers a wide range of features, including built-in functions and libraries, which makes it a popular choice among programmers. In this article, we will discuss 5 C++ example programs that are commonly used by programmers.

cpp example for beginner
1) Reverse the String
#include <iostream>
#include <string>
using namespace std;

int main() {
    string str;
    cout << "Enter a string : ";
    getline(cin, str); // read a line of text from the user

    int n = str.length();
    for (int i = 0; i < n / 2; i++) {
        // swap characters at opposite ends of the string
        char temp = str[i];
        str[i] = str[n - i - 1];
        str[n - i - 1] = temp;
    }

    cout << "Reversed string: " << str << endl;

    return 0;
}

Output

Enter a string: codinghelps
Reversed string: splehgnidoc

In this program, we first read a string from the user using the getline() function. Then we find the length of the string using the length() function and loop through half of the string (i.e., up to the middle character) using a for a loop. In each iteration of the loop, we swap the character at index i with the character at the opposite end of the string (i.e., index n – i – 1, where n is the length of the string).

After the loop completes, the entire string will have been reversed, so we simply print it out using cout.

2) Check String is Palindrome or Not
#include <iostream>
#include <string>
using namespace std;

int main()
{
    string str, reversed_str;
    cout << "Enter a string: ";
    getline(cin, str);
    int len = str.length();

    // Reverse the string
    for (int i = len - 1; i >= 0; i--)
    {
        reversed_str += str[i];
    }

    // Check if the original string and reversed string are equal
    if (str == reversed_str)
    {
        cout << "The string is a palindrome." << endl;
    }
    else
    {
        cout << "The string is not a palindrome." << endl;
    }

    return 0;
}

Output

Enter a string: codinghelps
The string is not a palindrome.

Enter a string: jkj
The string is a palindrome.

In this program, we first take a string as input from the user using getline(cin, str). We then find the length of the string using str.length() and store it in the variable len.

Next, we reverse the string by iterating from the last character to the first character using a for loop, and adding each character to a new string called reversed_str.

Finally, we check if the original string and the reversed string are equal using the == operator. If they are equal, then the string is a palindrome, and we print a message saying so. Otherwise, we print a message saying that the string is not a palindrome.

3) Sum of Digit in Number
#include <iostream>
using namespace std;

int main() {
    int num, digit, sum = 0;
    cout << "Enter an integer: ";
    cin >> num;
    while (num != 0) {
        digit = num % 10;
        sum += digit;
        num /= 10;
    }
    cout << "Sum of digits: " << sum << endl;
    return 0;
}

Output

Enter an integer: 123
Sum of digits: 6

Here’s how it works :

(1) The program asks the user to enter an integer.

(2) Inside the while loop, the program computes the last digit of the integer using the modulus operator (%), adds it to the sum, and then removes that digit from the integer by dividing it by 10.

(3) The while loop continues until there are no more digits left in the integer (i.e., the integer becomes 0).

(4) The program prints the sum of digits.

4) Count the Number of Words in a String
#include <iostream>
using namespace std;

int main() {
   string str;
   cout << "Enter a string: ";
   getline(cin, str);

   int wordCount = 0;
   for(int i = 0; i < str.length(); i++) {
      if(str[i] == ' ') {
         wordCount++;
      }
   }

   wordCount++;  // Add 1 for the last word
   cout << "Number of words in the string: " << wordCount << endl;

   return 0;
}

Output

Enter a string: coding helps
Number of words in the string: 2

This program prompts the user to enter a string using getline() a function and counts the number of words in the string by iterating over each character in the string and counting the number of spaces. The word count variable is incremented each time a space is encountered. Finally, the program adds 1 to wordCount including the last word, and outputs the result.

5) Check if the String Contains a Substring
#include <iostream>
#include <string>
using namespace std;

int main() {
    string str = "coding helps!";
    string sub = "helps";

    if (str.find(sub) != string::npos) {
        cout << "The string contains the substring" << endl;
    } else {
        cout << "The string does not contain the substring" << endl;
    }

    return 0;
}

Output

The string contains the substring

In this program, we declare a string str and a substring sub. We use the find() function of the string class to check if the substring is present in the string. If the find() function returns a value other than string::npos, it means that the substring is present in the string. If it returns string::npos, it means that the substring is not present in the string.