Problem Solving in C++: String Class

Mohammed Muzahidul Islam
4 min readMar 10, 2024

--

Introduction

Welcome to our weekly programming problem-solving series! Dive into our latest article as we tackle String Class challenges, offering insights, tips, and solutions to sharpen your coding skills. Whether you’re a beginner or a seasoned pro, join us on this journey of continuous learning and mastery in the world of programming.

Count Words

Given a string S. Print the number of words in it.

Word : consists of lowercase and uppercase English letters.

Input

Only one line contains S(1≤|S|≤10^6) where |S| is the length of the string and it consists of lowercase and uppercase English letters, spaces, and (‘!’, ‘.’, ‘?’ and ‘,’) symbols.

Output

Print the number of words in the given string.

Examples

input

Meep Meep!

output

2

input

I tot I taw a putty tat.

output

7

input

I did! I did! I did taw a putty tat.

output

10

input

Shsssssssssh ... I am hunting wabbits. Heh Heh Heh Heh ...

output

9

Try on your own first.

Photo by Caspar Camille Rubin on Unsplash

Answer:

#include<bits/stdc++.h>
using namespace std;

int countWords(string s) {
int count = 0;
bool inWord = false;

for (char c : s) {
if (isalpha(c)) {
if (!inWord) {
inWord = true;
count++;
}
} else {
inWord = false;
}
}

return count;
}

int main() {
string s;
getline(cin, s);

int result = countWords(s);

cout << result << endl;

return 0;
}

Reverse Words

Given a string S. For each word in S reverse its letters then print it.

Note: words are separated by space.

Input

Only one line contains a strings S (1≤|S|≤10^6) where |S| is the length of the string and it consists of lowercase and uppercase English letters, spaces.

Output

Print the answer required above.

Examples

input

I love you

output

I evol uoy

input

You love me

output

uoY evol em

input

We are a happy family

output

eW era a yppah ylimaf

Try on your first.

Photo by AltumCode on Unsplash

Answer:

#include <bits/stdc++.h>

using namespace std;

int main() {
string input;
getline(cin, input);

istringstream iss(input);
string word;
bool firstWord = true;

while (iss >> word) {
if (!firstWord) {
cout << " ";
}

for (int i = word.size() - 1; i >= 0; --i) {
cout << word[i];
}

firstWord = false;
}

cout << endl;

return 0;
}

Replace Word

Given a string S. Print S after replacing every sub-string that is equal to “EGYPT” with space.

Input

Only one line contains a string S (1≤|S|≤10^3) where |S| is the length of the string and it consists of only uppercase English letters.

Output

Print the result as required above.

Examples

input

BRITISHEGYPTGHANA

output

BRITISH GHANA

input

ITALYKOREAEGYPTEGYPTALGERIAEGYPTZ

output

ITALYKOREA  ALGERIA Z

Try on your own first.

Photo by Blake Connally on Unsplash

Answer:

#include<bits/stdc++.h>
using namespace std;

int main() {
string S;
cin >> S;

size_t pos = 0;
while ((pos = S.find("EGYPT", pos)) != string::npos) {
S.replace(pos, 5, " ");
}

cout << S << endl;

return 0;
}

Lucky?

A ticket is a string consisting of six digits. A ticket is considered lucky if the sum of the first three digits is equal to the sum of the last three digits. Given a ticket, output if it is lucky or not. Note that a ticket can have leading zeroes.

Input

The first line of the input contains an integer t (1≤t≤10^3) — the number of testcases.

The description of each test consists of one line containing one string consisting of six digits.

Output

Output t lines, each of which contains the answer to the corresponding test case. Output “YES” if the given ticket is lucky, and “NO” otherwise.

You can output the answer in any case (for example, the strings “yEs”, “yes”, “Yes” and “YES” will be recognized as a positive answer).

Example

input

5
213132
973894
045207
000000
055776

output

YES
NO
YES
YES
NO

Note

In the first test case, the sum of the first three digits is 2+1+3=62+1+3=6 and the sum of the last three digits is 1+3+2=61+3+2=6, they are equal so the answer is “YES”.

In the second test case, the sum of the first three digits is 9+7+3=199+7+3=19 and the sum of the last three digits is 8+9+4=218+9+4=21, they are not equal so the answer is “NO”.

In the third test case, the sum of the first three digits is 0+4+5=90+4+5=9 and the sum of the last three digits is 2+0+7=92+0+7=9, they are equal so the answer is “YES”.

Try on your own.

Photo by Michael Dziedzic on Unsplash

Answer:

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

int main() {
int t;
cin >> t;
while (t--) {
string ticket;
cin >> ticket;

int digit1 = ticket[0] - '0';
int digit2 = ticket[1] - '0';
int digit3 = ticket[2] - '0';
int digit4 = ticket[3] - '0';
int digit5 = ticket[4] - '0';
int digit6 = ticket[5] - '0';

if (digit1 + digit2 + digit3 == digit4 + digit5 + digit6) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
}

return 0;
}

Codeforces Problem Links:

  1. P. Count Words
  2. Q. Reverse Words
  3. V. Replace Word
  4. A. Lucky?

Please leave a Clap and Comment on my work. Your feedback will motivate me to create and share more content. Thank you!

--

--