Test: Strings (C++)

Test 6 of 8 · Tests

A test on working with text in C++ (string): reading a word and a whole line, going through the characters, upper and lower case letters, counting, comparing and changing characters. This page is new – the tasks are in two groups, with solutions.

Before you start: revise the lessons Strings in C++, Strings in C, and then do the tasks on the page Strings – examples.
How to practise: choose one group and solve it in 45 minutes, without looking at the solutions or your notes. Only after that compare your solution with the solution of the practice test.

What you need to know for the test

  • the difference between cin >> s (one word) and getline(cin, s) (a whole line)
  • the length s.length() and accessing a character s[i]
  • functions from <cctype>: isupper, islower, isdigit, tolower, toupper
  • that a character is actually a number: c - 'a' is the position of the letter, '0' + 5 is the character 5
  • joining strings (+) and searching (find)

What the test looks like

  • Duration: one school lesson (45 minutes).
  • Tasks: 3 (sometimes 4) tasks; every group has tasks of the same difficulty, but with different data.
  • What is graded: whether the program works correctly for the example and for edge cases, whether the input and output are as in the text and whether the code is clear.
Suggested scoring (3 tasks of 10 points each)
Points0–1112–1617–2122–2627–30
Grade12345

The scale is a suggestion for self-assessment; the teacher can adapt it. (In Serbian schools grades go from 1 to 5, where 5 is the best.)

Group 1

TASK 01Group 1

The number of vowels

Read one sentence (it may contain spaces) and print how many vowels it has (a, e, i, o, u – lower and upper case).

Example

Input

Programming is fun

Output

5
TASK 02Group 1

A palindrome sentence

Read a sentence and check whether it is a palindrome when spaces and the difference between upper and lower case letters are ignored. Print YES or NO.

Example 1

Input

Never odd or even

Output

YES

Example 2

Input

World of programming

Output

NO
TASK 03Group 1

The Caesar cipher

The Caesar cipher replaces every letter with the letter that is k places further in the alphabet (after z comes a again). Read k (0 ≤ k ≤ 25) and a word of lower case letters of the English alphabet, and print the encrypted word.

Example

Input

3
zebra

Output

cheud

Group 2

TASK 04Group 2

The number of words in a sentence

Read a sentence in which the words are separated by one or more spaces (there may also be spaces at the beginning and at the end) and print the number of words.

Example

Input

  Today   is a nice day 

Output

5
TASK 05Group 2

The longest word

Read a sentence and print its longest word. If there are several words of the same, greatest length, print the first one.

Example

Input

The world of programming is interesting

Output

programming

("programming" and "interesting" both have 11 letters, so the first one is printed.)

TASK 06Group 2

Checking a password

A password is strong if it has at least 8 characters, at least one upper case letter, at least one lower case letter and at least one digit. Read the password (without spaces) and print STRONG, or WEAK and below it everything that is missing.

Example 1

Input

Password123

Output

STRONG

Example 2

Input

secret

Output

WEAK
- too short
- no upper case letter
- no digit
Common mistakes and tips
  • After cin >> n the end of the line remains; if you then read with getline, first call cin.ignore().
  • Characters are compared with single quotes: s[i] == 'a', and strings with double quotes: s == "end".
  • To change the characters in a for (char &c : s) loop, you need the & sign.
  • To check "regardless of upper and lower case", convert everything to lower case before comparing.

Check your knowledge

Open the quiz

Mini quiz: strings

1. What does cin >> s read if the user enters Good day?




2. What is 'd' - 'a'?




3. What does s.length() return for s = "cat 1"?