0

i have this c++ code and i wondered how ot would be in c

    #include <vector> // for vector
    #include <sstream> // for stringstream
    #include <iostream>
    #include <string>

    using namespace std;

    int main()
    {
        stringstream ss;
        string input, temp;
        vector<string> comand;
        cout << ">: ";
        getline(cin, input);

        ss << input;    // brake up line and put each part into the vector
        while( ss >> temp)
        {
          comand.push_back(temp);
        }
            // display vector
        for(size_t i = 0; i < comand.size(); i++)
        cout << comand[i] << endl;

        cin.get(); // pause program
        return 0;
     }

what is does it takes in an input from the user and then brake it up into keywords and then display them again.

i used this type of program to make my own terminal program. so how is this code going to be in C

thanks....

3
Contributors
3
Replies
6
Views
4 Years
Discussion Span
Last Post by MasterHacker110
0

Yes, you would need to create a dynamic array of strings depending on how many are created from parsing the input string.
The program would count the strings and display them one-by-one.

0

call fgets() to get user input, then strtok() to break it up into individual words. I would just statically allocate the array of pointers to strings, for example char* strings[255] = {0}; . You could, as previously mentioned, also use a dynamically allocated array of pointers char** strings = 0, but then you need to call malloc() and realloc() to expand the array to the desired size each time you want to add a new string to it. For small amounts of strings its just simpler and better to just use static allocation.

0

Mmm thanks i understand this, thanks, i am more of a c++ guy. dont know why, just like the language.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.