Find first word in array

closed account (yhC2Nwbp)
Hi

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.

HB3YYF
Last edited on
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).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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;
}
Last edited on
closed account (yhC2Nwbp)
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.

1
2
3
for(iCount= 0; caTextInput[iCount] != ' '; iCount++);
	{
	}


When I have the length, I can create a second array (with that long) and "copy" the word for other using.
Can i make this more professional?

Thanks
HB3YYF
Last edited on
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?
Tell us what counts as part of a word. So far it's letters and numbers. How about punctuation and symbols?

What number do you want to get back from this (there's a space before the 'h'):
absg476£$;'_= hgr5 ?
Last edited on
closed account (yhC2Nwbp)
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.
Roll it into a std::string and use the find member function?

int len = std::string(textInput).find(' ')

No need to reinvent the wheel with ugly C functions, and these *are* the C++ forums, after all :)
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.
closed account (yhC2Nwbp)
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.
closed account (yhC2Nwbp)
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.
I think simplest to use peek():
1
2
3
4
5
6
7
8
9
10
11
12
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 (unsigned int 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
Last edited on
Topic archived. No new replies allowed.