Thread: Need this program to skip leading whitespace using isspace()

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    34

    Need this program to skip leading whitespace using isspace()

    I need function ParseStringFields() to skip leading white space in each delimited field created by the strtok function. I must use isspace() function. I am having a hard time with this? Help?

    Thanks...

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<ctype.h>
    #define BUFSIZE 255
    #define FILENAME "TestFile3.txt"
    
    FILE *OpenFile(const char *fileName);
    void ParseStringFields(FILE *fp);
    
    int main(void)
    {
       FILE *fp = OpenFile(FILENAME);
       ParseStringFields(fp);
       fclose(fp);
       return EXIT_SUCCESS;
    }
    
    FILE *OpenFile(const char *fileName)
    {
       FILE *myFilePointer;
       if((myFilePointer = fopen(FILENAME, "r")) == NULL)
       {
          fprintf(stderr, "Can't open "FILENAME"\n");
          exit(EXIT_FAILURE);
       }
       else
          return(myFilePointer);
    
    }
    
    void ParseStringFields(FILE *fp)
    {
       char *cp, buf[BUFSIZE];
    
       while (fgets(buf, (int)sizeof(buf), fp))
       {
          for(cp = buf; cp = strtok(cp, "AEIOUaeiou\n"); cp = NULL)
                puts(cp);
          puts(buf);
       }
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    So where's the part where you actually are trying to skip characters?


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

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    34
    I think it should be right before puts(cp) here:

    Code:
          for(cp = buf; cp = strtok(cp, "AEIOUaeiou\n"); cp = NULL)
                puts(cp);
          puts(buf);
    
    [

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Can you put into words how you would skip leading spaces? Break it down into steps, and tell me how you'd do it.


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

  5. #5
    Registered User
    Join Date
    Aug 2009
    Posts
    34
    Sure....

    strtok function tests each incoming char against a set of arbitrary delimiters (AEIOUaeiou\n), if said char is not one of these control falls to puts(cp). After it finds a delimiter it starts a new line, and begins printing char by char until it reaches another delimiter.

    If at the beginning of a new line there is leading white space, my program must skip that white space. It must use the isspace function.

    Now looking at my code it seems that the current for loop needs to be embedded in another loop which uses isspace to test char before control falls through to strtok function for loop?

    Code:
    void ParseStringFields(FILE *fp)
    {
       char *cp, buf[BUFSIZE];
       int loopCounter;
    
       while (fgets(buf, (int)sizeof(buf), fp))
       {
          for(cp = buf; cp = strtok(cp, "AEIOUaeiou\n"); cp = NULL)
                puts(cp);
             puts(buf);
       }
    }
    Hmm...Now I am even more lost.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So that's not really at all what strtok does. You should read the manual again to see if that's what you want to do, or whether you really want to check each letter, or what.

    puts doesn't really go letter by letter -- it prints a chunk of letters (from start to the end-of-string character).

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I was thinking more of something like this: "given a string, start with the first character, and as long as it's a space, skip it". You know, actual words on how you'd expect to skip it. I already know how to do it, I was hoping that if you could sort it out in actual words how you'd do it, that you'd figure out the code for it.

    That's usually the best way to do it. Be able to turn it into real words on what steps you'd take, the logic of it, and then turn those steps into code.


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  2. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  3. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM