2 Words

This is a discussion on 2 Words within the C Programming forums, part of the General Programming Boards category; If there was a list in which there were 2 words in each line. I need to print only the ...

  1. #1
    Ron
    Ron is offline
    Registered User
    Join Date
    May 2006
    Posts
    151

    Post 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.

  2. #2
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    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.
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    12
    start counting from begining and detect the first letter you hit then count until you hit the second space . then find out the word.

  5. #5
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,156
    Have you tried fscanf()?
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    12
    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;
    }

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  8. #8
    Ron
    Ron is offline
    Registered User
    Join Date
    May 2006
    Posts
    151

    Post

    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.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Gee it's sure good you put all that necessary information in your first post...


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  3. Problem with malloc() and sorting words from text file
    By goron350 in forum C Programming
    Replies: 11
    Last Post: 11-30-2004, 09:01 AM
  4. New Theme
    By XSquared in forum A Brief History of Cprogramming.com
    Replies: 160
    Last Post: 04-01-2004, 07:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21