I have a little problem. I have an character array (256 characters), which can be any text inside.
Now I would like to determine the length of the first word entered.
I can check when entering or using a loop. Are there maybe even a function?
What is the best method?
I work under Linux Debian with gnu gcc and SciTe as Editor.
On the assumption that a word is a block of letters, and the end of the word is identified by something that is not a letter, here's one way. It assumes that the first word begins with a letter in the first position (i.e. in caTextInput[0]). If that's not the case, you'll have to alter it. It doesn't do anything clever; it just counts along the array until it finds something that isn't a letter (or isn't the end of the C-style string i.e. isn't a zero - if you're not terminating the c-style string with a zero, you'll need to add a means to stop before going off the end of the array).
int getWordSize ( char* startOfArray)
{
int wordSize = 0;
char* currentLocation = startOfArray; // not strictly needed, but easier to understand by name
while ( *currentLocation != 0)
{
if (isAlpha(*currentLocation))
{
// this is a letter
wordSize++;
currentLocation++;
}
else
{ // not a letter
return wordSize;
}
}
// end of c-style string
return wordSize;
}
Thank you. My problem is that I do not know what the user enters there. It may be that the user sleep on the keyboard and something is pressed. So I need also to check the numbers.
My idea is not exactly the best, but the easiest. I use a for loop.
Assuming your array can be null-initialized and the text starts at the very first element in the array, you can simply subtract pointers to find out the length.
int size= strchr(_array, 0) - (_array) ;
This little piece of code finds the pointer to the first null character and subtracts it from the pointer to the start of the array. That's practically what you need, right?
I want to split a name and then search in a document to this. Now I have to check if a word has been entered or nothing. I think I can make this with a if function.
It may be that the user sleep on the keyboard and something is pressed. So I need also to check the numbers.
Junk in, junk out... Unless this is for school. On the other hand, I would be pretty upset if my name was "m8-the-gr8", and your program didn't let my apply for college or something.
I guess I would have to know more about the contents of this array. How is it being filled.
The program scanned many files for a specific text. I have many "eboock" as a *.txt file. So I should be able to search for something specific (a name of a author, a specific article, ... ).
One program scans all files. One search button and all will be searched.
It is private just for me.
My opinion, your program shouldn't do any validation (unless perhaps for an empty search array). You might want to find numbers, strings with more than one word, or any number of different things.
yes you're right. But with an empty string I can do no check. So I must first have an input.
Soon I have all the words in an array, I can using and / or to search for the text.
char words[10][100]; // store ten words with length 99 each
int position = 0; // index of first dimension
while (cin.peek() != '\n' && position < 10) // While user hasn't hit enter and in array bounds
{
cin >> words[position]; // extract the word, ignores white space
position++; // increase the index
}
//a cout
for (unsignedint i = 0; i < position; i++)
cout << words[i] << endl;
I don't know if google does this, but search engines used to be pretty rubust in options.
The default was or: Tom Petty, would result in pages with Tom or Petty
you could and: Tom and Petty, any page that had Tom and Petty
Quote: "Tom Petty", the exact phrase
Not: Tom not Jerry, pages that had Tom but not Jerry
And wildcard: *space, any page with a space or a word that ends with space