-
2 Words
If there was a list in which there were 2 words in each line. I need to print only the frist word.
In some cases, there is one or more spaces in front of the first word. What method would I have to use if I wanted to skip the space in front of the word and take only the first word?
Please advise.
-
Check out the functions listed in the ctype.h header file. Specifically ispunct() and isspace().
That would be one way, I'm not sure if there is another.
-
Try it for yourself. Walk through the string counting spaces until you hit a non-space. Then go until you hit a space. Stop then. There's your first word. Now let's see your code.
[edit]
You again is it?
[/edit]
Quzah.
-
start counting from begining and detect the first letter you hit then count until you hit the second space . then find out the word.
-
-
try this function
Code:
int GetFirstWord(char *line, char* firstword)
{
int i = 0, j =0;
while(line[i] <= ' ' ) /*Left trim*/
i++;
/*copy first characters > space (if you want only the letters type (line[i] > 'a'*/
while(line[i] > ' ' )
firstword[j++] = line[i++];
firstword[j] = '\0';
return j;
}
-
Why are you doing their home work for them? Do you think none of the four people before you that gave hints how to do it know how? Come on man! We don't just give out the full working code. How are they supposed to learn anything that way? For that matter, why are you using '<=' for white space comparison? Use isspace if you really want to test for white space. That's what it's designed for. It takes local into account.
Quzah.
-
Thanks vangmor. Actually I just needed a start and not the whole code. I was not supposed to use file functions as the others gave. I had to input the file using the UNIX operator '<'
So thanks again.
-
Gee it's sure good you put all that necessary information in your first post...
Quzah.