Click here to Skip to main content
15,890,825 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi so this is my program here to match sub strings.But I want to change the way input output works in my program.

First I want to ask the user how many strings he wants to input and then typing in the actual input.The output should be stored in an array and then displayed.I have tried many things but can't implement it.So any pointers on how to do it?

Example:

Input:

4
i love hackerrank
hackerrank is an awesome place for programmers
hackerrank
i think hackerrank is a great place to hangout


Output:
2
1
0
-1




This is my actual code.

C#
int main()
{

    vector<string> token_store;
    string s,token;



    getline(cin,s);
    std::istringstream iss(s);


    while(iss>>token)
        token_store.push_back(token);   //splitting strings into tokens


    int len=token_store.size();        //storing size of vector

    if(token_store[0]=="hack" && token_store[len-1]=="hack")cout<<"first and last same";     //if first and last word of input string is hack
    else if(token_store[0]=="hack")cout<<"first matches";                                    //if first word of input string is hack
    else if(token_store[len-1]=="hack")cout<<"last matches";                                 //if last word of input string is hack

}
Posted
Comments
Sergey Alexandrovich Kryukov 16-Oct-13 12:03pm    
Why do you think it would require running the program again? Yes, it is possible, but, unlike other things you did, it would not be portable between different platforms, so I would rather advise to avoid it.
—SA
Ratul Jain 16-Oct-13 12:50pm    
It's just a learning exercise and the code should be in the abovementioned format only for the computer to test it's test cases on it.

It's the requirement of the question and of many other questions and I am having a hard time implementing it.
Sergey Alexandrovich Kryukov 16-Oct-13 12:56pm    
I gave you my recommendations, strongly advise to follow it.
As to the learning exercise, I'll gladly help you there, too, but you need to clarify your problems. First, you would need to answer my question on "running again"...
—SA
Ratul Jain 16-Oct-13 14:01pm    
I am really sorry but I don't understand your question completely.Why I want my program to run again(or asking for a string again)?Which I have answered above that the question asks so.I must be missing something here I guess. :(
Sergey Alexandrovich Kryukov 16-Oct-13 14:13pm    
As I say, I don't understand how your requirements can possibly be related to restarting of the program. What's your idea?
—SA

1 solution

Please see my comment to the question and try to review your requirements to the functionality.

You have at least two problems in your suggestions. First, as I say, it's not clear why the second run would be required. It is never a nice option.

The second problem is: the users would not like the idea of writing how many string she/he wants to input. It would be a perfect requirement for some code, but human beings prefer to change their minds and get more freedom. The usual approach is to suggest the user to input a "special string" to indicate "end of input", for example: "Enter next line, empty line (press Enter) to exit".

But even this is not nice. A nice application can be either with some GUI, or a console-only. But if this is a console-only application, its not nice to have it interactive, as it would loose all the benefits of a console-only application (and one of the main benefits is the possibility to run if from a batch file, otherwise GUI is better).

So, what should be the requirements for such a nice/cultured console-only program? Simple. All input should be in one command line. If such input tends to be too big, one or more of the command-line arguments may expect one or more file names specified, and those files can be used to input more detail. Only in this approach, the console-only application with variable user input can be really convenient. Please see:
http://en.wikipedia.org/wiki/Main_function#C_and_C.2B.2B[^],
http://en.wikipedia.org/wiki/Command-line_argument#Arguments[^],
http://en.wikipedia.org/wiki/Batch_file#Windows_NT[^].

—SA
 
Share this answer
 
v2
Comments
Manfred Rudolf Bihy 16-Oct-13 12:18pm    
Well said, SA. 5+
Sergey Alexandrovich Kryukov 16-Oct-13 12:52pm    
Thank you, Manfred.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900