Code: Show/Hide char* GetWords(char* input) { std::stringstream ss(std::stringstream::in|std::stringstream::out); ss<<input; int words = GetNumberOfWords(input); char* wordlist[words]; for(int i=0;i<words;++i)ss>>wordlist[i]; return wordlist; } |
tcsoccerman wrote: |
google search="c++ function array return" first link.
int *foo(int *array) { int *start = array; return start; } int main() { int myarray[] = {1,2,3,4,5,0}; foo(myarray); return 0; } output="1,2,3,4,5" |
Quote: |
vector <string> GetWords(char* input)
{ std::stringstream ss(std::stringstream::in|std::stringstream::out); ss<<input; int words = GetNumberOfWords(input); vector <string> wordlist; for(int i=0;i<words;++i){ string s; ss>> s; wordlist.push_back(s); return wordlist; } |
Bak wrote: | ||
You could be more efficient by using the heap directly, although I'm not sure how good you are with dynamic memory . |
Code: Show/Hide expected constructor, destructor, or type conversion before '<' token expected `,' or `;' before '<' token |
Code: Show/Hide vector <string> GetWords(char* input) |
Code: Show/Hide char** GetWords(char* input) { std::stringstream ss(std::stringstream::in|std::stringstream::out); ss<<input; int words = GetNumberOfWords(input); char** wordlist = malloc((words + 1) * sizeof(char*)); wordlist[words] = 0; // so we know when to stop for(int i=0;i<words;++i){ string s; ss >> s; wordlist[i] = malloc(s.length() + 1); strcpy(wordlist[i],s.c_str()); } return wordlist; } |
Code: Show/Hide char ** words = GetWords(input); for (int x = 0; words[x] != 0; ++x) { cout << "word " << x << " is " << words[x] << endl; free(words[x]); } free(words); |
Code: Show/Hide int* x, y; |
tcsoccerman wrote: |
x is y isn't. if i remember correctly. |
Dr Brain wrote: |
That's why, style wise, you should always put a space between the type and the *. So int *x; or char *string;. So that you're not confused when declaring multiple pointers on the same line. |
Bob Dole.. Bob Dole... Bob Dole...... bob dole.... bob... dole.... wrote: | |
Bak did that, so you don't have to keep track of how many items you got inside the arrray.
Otherwise, you'd always have to know the exact amount of words when you want to iterate over it. With bak's way, you can just stop if you reach 0 (the last word). Take a look at his way to free the array. Edit: While we are at it. I think, when you declare a pointer, you should keep the * to the variable name. I know it's about personal preference here. What do you think you are declaring when you do:
Hint: It's not two pointers. |
Code: Show/Hide #include <iostream>
#include <stdlib.h> #include <string> #include <sstream> using namespace std; #define ENDOFLIST "54gf54g5sf8^&*78d" int GetNumberOfWords(string buf) { int words = 0; int length = buf.length(); for(int i=0;i<length;++i) { if(buf[i]!=' ' && i==0)++words; if(buf[i]==' ' && i!=0) { if(buf[i+1]!=' ')++words; if(i>1 && buf[i-1]==' ' && buf[i-2]==' ' && buf[i+1]!=' ' && words==0)++words; } } return words; } string *GetWords(string input) { std::stringstream ss(std::stringstream::in|std::stringstream::out); ss<<input; int words = GetNumberOfWords(input); string *wordlist = new string[words+1]; wordlist[words] = ENDOFLIST; for(int i=0;i<words;++i) { string s; ss>>s; wordlist[i]=s; } return wordlist; } int main() { string buf; cout<<"Enter a string: "; getline(cin,buf); cout<<"Number of words: "<<GetNumberOfWords(buf); string *wordlist = GetWords(buf); cout<<"\nList of words:"; { int i = 0; while(wordlist[i]!=ENDOFLIST) { cout<<endl<<wordlist[i]; ++i; } } delete[] wordlist; cout<<endl; system("pause"); } |