Thread: something like parsing...i think

  1. #1
    hmm...
    Guest

    Unhappy something like parsing...i think

    if a function is sent a string like : My name is Bob. ; how could you separate each word? w/o storing each word in a new array? is there a way to get the first word, make a change to it, then print it with the change and then move on to the next word in the array? how could you do this without using anything but stdio.h?

    i was thinking of something like using a for loop ?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Read the data. Modify it. Display it. Rinse. Repeat.

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

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    Look up strstr and strchr.

    Hint: char *

  4. #4
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>Look up strstr and strchr.
    That's not in stdio.h, they're in string.h.

    >> how could you do this without using anything but stdio.h?
    Use sscanf, since it only reads up to the next whitespace you can break up words easily. The biggest problem is how to move the get pointer forward through the string using it as a loop. This can be done by using the %n format for sscanf that tells you how many characters were read, just assign a pointer to the array and after each repetition of the loop, add the value of %n to the pointer until there's nothing left.
    Code:
    #include <stdio.h>
    
    int main(void)
    {
      int n;
      char name[] = "My name is Bob", *namep = name;
      char word[5];
    
      while (sscanf(namep, "%s%n", word, &n) == 1)
      {
        word[0] = 'A';
        printf("%s\n", word);
        namep += n;
      }
    
      return 0;
    }
    *Cela*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need sth about parsing
    By Masterx in forum C++ Programming
    Replies: 6
    Last Post: 11-07-2008, 12:55 AM
  2. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  3. draw tree graph of yacc parsing
    By talz13 in forum C Programming
    Replies: 2
    Last Post: 07-23-2006, 01:33 AM
  4. Parsing for Dummies
    By MisterWonderful in forum C++ Programming
    Replies: 4
    Last Post: 03-08-2004, 05:31 PM
  5. I hate string parsing with a passion
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 03-19-2002, 07:30 PM