Thread: sscanf problem

  1. #1
    Registered User
    Join Date
    Dec 2006

    sscanf problem

    I'm really new to C Programming. We have an assignment where the program is supposed to accept a string input, and "decode it". So, if the user enters in 3a4g5s at the prompt, the output will be AAAGGGGSSSSS, for example. I elected to do this with for loops. This is my encode function:

    void decode(char *ostr, int lenght)
      char nstr[lenght];  //new shortened copy of the string
      int char1; //temporary character
      int lcv = 0; //loop control variable
      int lcv2 = 0; //loop control variable
      int counter = 0; //counter variable
      int counter2 = 0;
      strcpy(nstr, ostr);
      char1 = nstr[1];
      counter = (int)nstr[0];
      for(lcv = 2; lcv <= lenght; lcv=lcv+2)
        for(lcv2=0; lcv2 < counter; lcv2++)
          printf("%c", toupper(char1));
        char1 = nstr[lcv+1];
        counter = (int)nstr[lcv];
    Now, the problem with this is that when it scans in a value for counter, it is scanning in the ASCII value of the number, rather than the number itself. I don't know how to force it to pull the number out of the string.

    My thought was to use sscanf, where the command would be:
      sscanf(nstr, "%d", &counter);
    The problem with this is that even in the for loop, it will only keep grabbing the first number in the string, rather than moving down the string as the for loop increments. Does anyone here have any suggestions?

  2. #2
    Registered User
    Join Date
    Nov 2006
    have you tryed using the atoi function

  3. #3
    Registered User
    Join Date
    Aug 2005
    You can do something like this
    int n, d;
    sscanf(buffer, "%d%n", &d,&n);
    So after reading an int "%n" will cause n to hold the number of characters read so far.
    In the next iteration you could advance the buffer pointer by n.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    You can use %n to get scanf() to store the position it read til, but I don't think that's realy what you're looking for.

    If you just want to convert the letter '1' into the number 1, subtract '0' from it.
    char c = '1';
    int n = c - '0';
    For multiple digits, you can multiply the whole thing by 10 before converting each digit.

    BTW, "lenght" is spelled "length".

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell

    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ:

    My website:
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Dec 2006
    Thanks, subtracting '0' worked perfectly!

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Quote Originally Posted by LordShme
    Thanks, subtracting '0' worked perfectly!
    I suppose it will until you run into a multi-digit number such as 13a24g35s.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  2. problem with sscanf
    By nevrax in forum C Programming
    Replies: 19
    Last Post: 04-14-2007, 10:59 PM
  3. Weird problem with sscanf
    By g4j31a5 in forum C++ Programming
    Replies: 17
    Last Post: 10-04-2006, 09:16 PM
  4. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 05:46 PM