Idea for an exercise, troule figuring out the solution though.

So I was thinking of ways to improve my c++ skills and I thought up this problem:

"Create a program where a user enters a string, have the program ask how many characters the user wants read from the string. Pass the string & the number of letters the user wants read to a function which returns a new string consisting only of the number of characters the user wants read."

I.E.

Enter a string :
The dog was awesome.
Enter # of characters you want read into new string :
7
New string : The dog


I could do this with a character array but I'm having trouble figuring a way to do it with a string variable. Any tips anyone?
str.substr(0,numberOfCharacters);
See http://www.cplusplus.com/reference/string/string/substr/
closed account (DSLq5Di1)
The solution for a character array would be no different for a string.. though you have more tools at your disposal when using a string.
Topic archived. No new replies allowed.