0% found this document useful (0 votes)
3 views5 pages

Essential String Manipulation Techniques

The document contains a series of basic coding questions and their corresponding C++ solutions. It covers how to reverse a string, check for palindromes, count numerical characters, find occurrences of a specific character, and identify non-matching characters in a string. Each question is accompanied by a complete code example demonstrating the solution.

Uploaded by

kumarshubham-ecs
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

Essential String Manipulation Techniques

The document contains a series of basic coding questions and their corresponding C++ solutions. It covers how to reverse a string, check for palindromes, count numerical characters, find occurrences of a specific character, and identify non-matching characters in a string. Each question is accompanied by a complete code example demonstrating the solution.

Uploaded by

kumarshubham-ecs
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Basic coding questions

Q1. How do you reverse a string?

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int main()
{
cout << "How do you reverse a string?" << endl;
string name = "shubham";

// Method 1: Using std::reverse from <algorithm>


string method1 = name; // Create a copy for
demonstration
reverse([Link](), [Link]());
cout << "This is method 1 (using std::reverse): " <<
method1 << endl;

// Method 2: Using a for loop (printing in reverse


order)
cout << "This is method 2 (using a for loop): ";
for (int i = [Link]() - 1; i >= 0; i--) {
cout << name[i];
}
cout << endl;

// Method 3: Using a temporary string


string method3;
for (int i = [Link]() - 1; i >= 0; i--) {
method3.push_back(name[i]);
}
cout << "This is method 3 (using a temporary string): "
<< method3 << endl;

// Method 4: Using two-pointer approach with std::swap


string method4 = name; // Create a copy for
demonstration
int left = 0, right = [Link]() - 1;
while (left < right) {
swap(method4[left], method4[right]);
left++;
right--;
}
cout << "This is method 4 (using std::swap): " <<
method4 << endl;

return 0;
}

Q2. How can you check whether a string is a palindrome or not?

#include <iostream>
#include <string>
using namespace std;

int main()
{
cout << "Enter your text : ";
string text;
getline(cin, text); // input from user

int len = [Link]();


bool isPalindrome = true; // Assume it's a palindrome

for (int i = 0; i < len / 2; i++)


{
if (text[i] != text[len - 1 - i])
{
isPalindrome = false;
break;
}
}

if (isPalindrome)
{
cout << "Palindrome" << endl;
}
else
{
cout << "Not Palindrome" << endl;
}
return 0;
}
Q3. How can you compute the number of numericals in a string?

#include <iostream>
#include <string>
using namespace std;

int countNumericals(const string& str) {


int count = 0;
for (char ch : str) { // Iterate through each character
in the string
if (isdigit(ch)) { // Check if the character is a
digit
count++;
}
}
return count; // Return the total count of digits
}

int main() {
cout << "Enter a string: ";
string input;
getline(cin, input); // Read the entire line including
spaces

int numericalCount = countNumericals(input); // Count


the digits in the string
cout << "Number of numericals in the string: " <<
numericalCount << endl;

return 0;
}

[Link] do you find the count for the occurrence of a particular character in string?

#include <iostream>
#include <string>
using namespace std;

int countOccurrences(const string& str, char ch) {


int count = 0;
for (char c : str) { // Iterate through each character
in the string
if (c == ch) { // Check if the character matches
count++;
}
}
return count; // Return the total count
}

int main() {
cout << "Enter a string: ";
string input;
getline(cin, input); // Read the entire line including
spaces

cout << "Enter the character to find: ";


char target;
cin >> target;

int count = countOccurrences(input, target);


cout << "The character '" << target << "' occurs " <<
count << " times in the string." << endl;

return 0;
}

Q5. How do you find the non-matching characters in a string?

#include <iostream>
#include <string>
#include <set>
#include <algorithm>
using namespace std;

string findNonMatching(const string& str, const string&


reference) {
set<char> strSet([Link](), [Link]());
set<char> refSet([Link](), [Link]());
string result;
set_difference([Link](), [Link](),
[Link](), [Link](), back_inserter(result));
return result;
}

int main() {
cout << "Enter the main string: ";
string mainString;
getline(cin, mainString);

cout << "Enter the reference string: ";


string reference;
getline(cin, reference);

string result = findNonMatching(mainString, reference);


cout << "Non-matching characters: " <<
([Link]() ? "None" : result) << endl;

return 0;
}

You might also like