Stuck....

This is a discussion on Stuck.... within the C++ Programming forums, part of the General Programming Boards category; I think I tried to post it here before but it didn't work. Im not askin for the full solution, ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    1

    Unhappy Stuck....

    I think I tried to post it here before but it didn't work. Im not askin for the full solution, the deadline is passed anyway but this has been keeping me up nights.

    "Write a function scanstring that works like scanf with a %s placeholder - it should skip leading whitespace and copy a string up to the next whitespace character. It should have 2 parameters: the string variable into which the string is copied, and the amount of space available in the string. It should use getchar to read in the string characters.
    "

    This is an assignment I was given, I couldnt figure out how to test for whitespaces and I don't know how to use the Cstring::trimleft...


    Any help with either would be appreciated.

  2. #2
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    You could use std::string, and use the find_first_not_of member function to find the first position that is not a whitespace, and then use the find_last_of member function to find the last character that is not a whitespace. Then you could use the substr member function to create a substring based on the two positions obtained to create a trimmed string. You could also write some predicate function for the transform algorithm which would trim strings also, but since I don't have a compiler or any of my code in front of me, that will be up to you to figure out. Both ways work.
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  3. #3
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Function declaration
    void Scanstring ( char a[], int size);

    Code:
    int ch;
    
    while( (ch = getchar() ) = '\n' || size != 0)
    {
      //skip whitespace
      if ( ch == ' ' ) continue;
      else
      {
      //Than add ch characters to the array a[]
      -- size;
      }
    }
    I believe that this is the general idea although simplified and not complete.
    Last edited by Troll_King; 10-17-2002 at 08:45 PM.

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I'm not sure if it was you or someone else, but I do remember a question similar to this. Do a search, although I'm not sure what you would search.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 10-23-2006, 08:22 PM
  2. string array stuck:(
    By mass in forum C Programming
    Replies: 18
    Last Post: 05-22-2006, 05:44 PM
  3. Program stuck in infinite loop-->PLEASE HELP
    By Jedijacob in forum C Programming
    Replies: 5
    Last Post: 03-26-2005, 12:40 PM
  4. Stuck on random generating
    By Vegtro in forum C++ Programming
    Replies: 3
    Last Post: 10-01-2003, 08:37 PM
  5. stuck ky
    By JaWiB in forum Tech Board
    Replies: 2
    Last Post: 06-15-2003, 09:28 PM

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