Hi I am using Dev-C++ and am trying to create a function that will parse a given string and keep searching each letter until it finds either a space or a NULL character.
It then has to copy that word and return it. Basically a user inputs a list of word e.g. "hi my name is" and this function is called to seperate the sentance into words and store them in an array.
For some reason it does not want to work and I have tried everyway i can think of :(
The code is below, thank you for any help you can suggest
1 2 3 4 5 6 7 8 9 10 11 12 13 14
string word_return(string input, int *start) //define starting word to check with given start point
{
int i = *start; //position to start from
string cpy =""; //what part of the word to copy
while (input[i]!=" " || input[i]!="\0") //while letter is not a space or a NULL char
{
i++;;
}
input.copy(cpy,start,i-1); //copy selected portion of string from input to cpy
//cpy[length] = "\0"; //add null char
cout <<"i:"<< i << " \tCopy:"<< cpy<<"\tinput: "<<input<<"\tstart: "<<start<<endl;
return cpy;
}
For some reason it wont allow this to work no matter what i try :(
This gives the following error message:
114 C:\Users\Chazz & Bill\Documents\prog\input\main.cpp ISO C++ forbids comparison between pointer and integer
string FindWord(string SearchIn, unsignedint FindWord)
{
string Final = "";
unsignedint Found = 0;
for(unsignedint i = 0; i < SearchIn.length(); i++)
{
if(SearchIn[i] != ' ') //This line corrected from " " to ' ' as mentioned below
{
Final += SearchIn[i];
}
else
{
Found++;
if(Found == FindWord)
{
return(Final);
}
else
{
Final = "";
}
}
}
return("");
}
All this does it look in SearchIn and finds word # FindWord. FindWord works on a 1-based index.
It shouldn't take much modification to put in array, for instance the else{ Final = ""; } would be where to add the word to the array before clearing it.
130 C:\Users\Chazz & Bill\Documents\prog\input\main.cpp ISO C++ forbids comparison between pointer and integer
which is the line:
if(SearchIn[i] != " ")
for some decrepid reason it just doesnt allow searching of individual characters within a string :S
It shouldn't take much modification to put in array, for instance the else{ Final = ""; } would be where to add the word to the array before clearing it.
the return value is where it is sent into the array. that can be easily sorted when i get there XD so long as it returns the word and updates the start position for next word (so i will need to use pass by reference as opposed to value for int FindWord).
well ill be dammed it worked :| thats actually the only problem??? my compiler does not regard single quotes to be the same as doubles for single chars :(
really? i thought if you were checking each character within a string it would not matter. it just seems like lazy programming to not include it tbh XD ok thank you :)
It's how the language differentiates between a string of one character and a single character.
A way to look at it: "a" == {'a', '\0'}
As using double quotes creates a C-style null-terminated string. 'a' == {'a'}
Using single quotes refers to only a character.