You are not logged in. Please login at www.codechef.com to post your questions!

×

storing strings help c++ beginner

The problem is concerned with text occurrence and Im having problems with storing inputs.

INPUT:
abc defghi jkl
bag pencil bag notebook

OUTPUT:
abc 0
defghi jkl

bag 1
pencil bag notebook

INPUT EXPLAINED: It is to understood that the first word in the input is the "KEYWORD" to be search in a set of strings and the following set of strings are the words where you will search the occurrence of the "KEYWORD".
OUTPUT EXPLAINED: The output consists of the "KEYWORD" followed by the number of its occurrence in the set of strings then next line consists of the set of strings.


so I was experimenting with this code

    string text;
    string key;
while(cin >> key && getline(cin, text, 'n'))
{
    cout << key << endl;
    cout << text << endl;
}

Its just an experimental draft on how to get the words to be placed on the string but it doesn't work. Please suggest a way on how to get strings for that kind of problem.

asked 16 Nov '14, 12:57

ramher237's gravatar image

2★ramher237
1351619
accept rate: 28%


int main()
{
    string key, text;
    while(cin >> key && cin.get() && getline(cin, text))
    {
        cout << key << endl;
        cout << text << endl;
    }
    return 0;
}
link

answered 06 Dec '14, 16:04

priyanshg's gravatar image

1★priyanshg
161
accept rate: 33%

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×1,901
×348
×193

question asked: 16 Nov '14, 12:57

question was seen: 810 times

last updated: 06 Dec '14, 16:04