Category: C++ Interview Questions

https://cdn3d.iconscout.com/3d/premium/thumb/interview-question-3d-icon-download-in-png-blend-fbx-gltf-file-formats–interviewer-hr-manager-pack-business-icons-9684875.png

  • Write a Program to Remove All Characters From a String Except Alphabets

    C++// C++ Programto remove all characters from a string except // alphabets #include <cctype> #include <iostream> #include <string> using namespace std; string remove_non_alphabets(string str) { string result = ""; for (char c : str) { if (isalpha(c)) { result += c; } } return result; } int main() { string str = "Gee$ksfor$geeks"; cout << "Alphabets only: " << remove_non_alphabets(str) << endl; return 0; }

    Output

    Alphabets only: Geeksforgeeks
  • Write a Program to Remove the Vowels from a String

    C++// C++ Program to remove the vowels from a string #include <cstring> #include <iostream> using namespace std; int main() { int j = 0; string str = "GeeksforGeeks"; for (int i = 0; str[i] != '\0'; i++) { if (str[i] != 'a' && str[i] != 'e' && str[i] != 'i' && str[i] != 'o' && str[i] != 'u' && str[i] != 'A' && str[i] != 'E' && str[i] != 'I' && str[i] != 'O' && str[i] != 'U') { str[j++] = str[i]; } } while (j < str.size()) { str[j] = '\0'; j++; } cout << "String without vowels: " << str << endl; return 0; }

    Output

    String without vowels: GksfrGks
  • Write a Program to Count the Number of Vowels 

    C++// C++ Program to count the number of vowels #include <cstring> #include <iostream> using namespace std; int main() { string str = "GeeksforGeeks to the moon"; int vowels = 0; for (int i = 0; str[i] != '\0'; i++) { if (str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' || str[i] == 'u' || str[i] == 'A' || str[i] == 'E' || str[i] == 'I' || str[i] == 'O' || str[i] == 'U') { vowels++; } } cout << "Number of vowels in the string: " << vowels << endl; return 0; }

    Output

    Number of vowels in the string: 9
  • Write a Program to Toggle Each Character in a String 

    C++// C++ Program to toggle string #include <cstring> #include <iostream> using namespace std; int main() { string str = "GeeksforGeeks"; for (int i = 0; str[i] != '\0'; i++) { if (islower(str[i])) { str[i] = toupper(str[i]); } else if (isupper(str[i])) { str[i] = tolower(str[i]); } } cout << "Toggled string: " << str << endl; return 0; }

    Output

    Toggled string: gEEKSFORgEEKS

  • Write a Program to Find the Length of the String Without using strlen() Function 

    C++// C++ Program to find the length of a string without using // strlen() #include <cstring> #include <iostream> using namespace std; int main() { string str = "GeeksforGeeks"; int length = 0; for (int i = 0; str[i] != '\0'; i++) { length++; } cout << "The length of the string is: " << length << endl; return 0; }

    Output

    The length of the string is: 13
  • Write a Program to Print Check Whether a Character is an Alphabet or Not

    C++// C++ program to print whether a character is an alphabet // or not #include <cctype> #include <iostream> using namespace std; int main() { char ch; ch = 'a'; if (isalpha(ch)) { cout << ch << " is an alphabet." << endl; } else { cout << ch << " is not an alphabet." << endl; } return 0; }

    Output

    a is an alphabet.
  • Write a Program to Check Whether a Character is a Vowel or Consonant

    C++// C++ Program to print whether a character is vowel or not #include <cctype> #include <iostream> using namespace std; int main() { char ch = 'e'; if (isalpha(ch)) { if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' || ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') { cout << ch << " is a vowel." << endl; } else { cout << ch << " is a consonant." << endl; } } else { cout << ch << " is not an alphabet." << endl; } return 0; }

    Output

    e is a vowel.
  • Write a Program to Find the ASCII Value of a Character

    C++// C++ Program to find ASCII value of a character #include <iostream> using namespace std; int main() { char ch; ch = 'A'; cout << "The ASCII value of " << ch << " is " << int(ch) << endl; return 0; }

    Output

    The ASCII value of A is 65
  •  C++ Program To Check Whether Number is Even Or Odd

    C++// C++ program to check // for even or odd #include <iostream> using namespace std; // Returns true if n is // even, else odd bool isEven(int n) { return (n % 2 == 0); } // Driver code int main() { int n = 247; if (isEven(n) == true) { cout << "Even" << endl; } else { cout << "Odd"; } return 0; }

    Output

    Odd
  • Write a Program to Find the Greatest of the Three Numbers.

    C++// C++ program to find greatest // among three numbers using #include <iostream> using namespace std; int main() { int a = 10, b = 20, c = 30; cout << "The Greatest Among Three Numbers is : "; if (a >= b && a >= c) { cout << a << endl; } else if (b >= a && b >= c) { cout << b << endl; } else { cout << c << endl; } return 0; }

    Output

    The Greatest Among Three Numbers is : 30