You are here:

C++/i need help in my assignment

Advertisement


Question
my assignment talking about string manipulation and he needs a menu of 11 :
1.   Inputting new string
2.   Display the content of the current string
3.   Erasing the content of the current string
4.   Adding more text to the end of the string
5.   Count the number of words in the string
6.   Count the number of characters in the string
7.   Search for a word in the string
8.   Count the number of times a word exists in the string
9.   Turn the string content to upper case.
10.   Turn the string content to lower case.
11.   Exit
he wants every string make a certain function

1-   Inputting new string functionality will allow the user to input a new string to process.
2-   Display the content of the current string functionality displays the current string stored in the system.
3-   Erasing the content of the current string functionality clears the current string.
4-   Adding more text to the end of the string functionality allows the user to input more string to append at the end of the current string.
5-   Count the number of words in the string functionality will print the number of words in the current string.
6-   Count the number of characters in the string functionality will print the number of characters in the current string.
7-   Search for a word in the string functionality will take a word from the user and print either: “Word was not found in the string ” or “Word was found in the string .
8-   Count the number of times a word exists in the string functionality takes from the user a word and prints the number of times the word exists in the string as a separate word and returns to the menu.
-------------------------------------------------------------------------------------
9-   Turn the string content to upper case functionality will re-write the transform the current string to upper case and returns to the menu.
---------------------------------------------------------------------------
10-   Turn the string content to lower case functionality will re-write the transform the current string to upper case and returns to the menu.
---------------------------------------------------------------------------
11-   Exit terminates the program

I don't how to use strings or functions


Answer
1. Inputting new string

assuming that 'Inputting new string' means read a line of text, use std::getline()
       std::string str ;
       std::getline( std::cin, str ) ;


2. Display the content of the current string

std::string is streamable. std::cout << str << '\n' ;


3. Erasing the content of the current string

str.clear() ;

4. Adding more text to the end of the string

str += "more text"

5. Count the number of words in the string

using a stringstream is the easiest way to do this (#include <sstream>) :

  std::istringstream stm(str) ;
  std::string word ;
  int number_of_words = 0 ;
  while( stm >> word ) ++number_of_words ;


6. Count the number of characters in the string

str.size() gives the number of characters


7. Search for a word in the string

use string::find() http://www.cppreference.com/wiki/string/find


8. Count the number of times a word exists in the string

invoke string::find() repeatedly till string::npos is returned

9. Turn the string content to upper case.

invoke std::toupper() on each char in the string.
http://www.cppreference.com/wiki/c/string/toupper


10. Turn the string content to lower case.

invoke std::tolower() on each char in the string.
http://www.cppreference.com/wiki/c/string/tolower  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


vijayan

Expertise

my primary areas of interest are generic and template metaprogramming, STL, algorithms, design patterns and c++11. i would not answer questions about gui and web programming.

Experience

about 15 years or so

Education/Credentials
post graduate engineer

©2016 About.com. All rights reserved.