Category: C++ Interview Questions
-
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 <<…
-
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’ &&…
-
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] ==…
-
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;…
-
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…
-
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.” <<…
-
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…
-
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
-
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” <<…
-
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…