I am struggling with getting my function to find either a space, new line, or dash ( - ) in my string. I was able to write it to find a space with no problem, but when I try to add a new line (\n) or a dash (-) to the search parameters, it doesn't work. I am basically writing two functions: one to find the length of a word, and one to find the beginning of a word. These are both used later as a part of a more complex function. What I have that works:
//start of word function
int word_start(int start, string paragraph)
{
int now_start;
now_start= paragraph.find_first_not_of(" ", start);
return now_start;
}
//length of word function
int word_length(int start, string paragraph)
{
int length;
int end;
end= paragraph.find(" ", start);
if(end==-1)
{
end= paragraph.find_last_not_of(" ");
length= end - start +1;
}
else
length= end- start;
return length;
}
When I try to change it to search for dashes and new lines, I write:
//start of word function
int word_start(int start, string paragraph)
{
int now_start;
now_start= paragraph.find_first_not_of("- \n", start);
return now_start;
}
//length of word function
int word_length(int start, string paragraph)
{
int length;
int end;
end= paragraph.find("- \n", start);
if(end==-1)
{
end= paragraph.find_last_not_of("- \n");
length= end - start +1;
}
else
length= end- start;
return length;
}
This doesn't work. I definitely feel this is a syntax error; however I haven't been able to figure out how to write this correctly. All the examples I have found say that when searching for multiple characters to write them all mashed together like
paragraph.find_last_not_of("aeiou")
which I have been trying to replicate with " ", - and \n but with no success. Any insight would be most appreciated!!
I figured it out! To find one of two (or several) different characters, you need to use the find_first_of function, not the find function! Using find searches for the exact phrase within the quotes, while the find_first_of function searches for the first character that matches any of the ones specified in the quotes.
//start of word function
int word_start(int start, string paragraph)
{
int now_start;
now_start= paragraph.find_first_not_of("- ", start);
return now_start;
}
//length of word function
int word_length(int start, string paragraph)
{
int length;
int end;
end= paragraph.find_first_of(" -", start);
if(end==-1)
{
end= paragraph.find_last_not_of("- ");
length= end - start +1;
}
else
length= end- start;
return length;
}