slipped my mind ... cleaning up strings

This is a discussion on slipped my mind ... cleaning up strings within the C++ Programming forums, part of the General Programming Boards category; OK ... if i read in a string, how do i get rid of any leading/trailing whitespace? here's how i ...

  1. #1
    edk
    edk is offline
    Registered User
    Join Date
    Sep 2001
    Posts
    20

    slipped my mind ... cleaning up strings

    OK ... if i read in a string, how do i get rid of any leading/trailing whitespace? here's how i am reading in the string...

    Code:
    const int maxStrLen(100);
    .
    .
    .
    char userInput[maxStrLen];
    cin.getline(userInput, maxStrLen, '\n');
    .
    .
    .
    So, how do I get rid of leading or trailing whitespace? whitespace includes the space character as well as tab.
    liberate tutamet ex inferis

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    loop through the array and use isspace() to check for whitespace and when you get a non-space char move them into a second array until the first '\0' . there you have it!
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    edk
    edk is offline
    Registered User
    Join Date
    Sep 2001
    Posts
    20
    you may be a stoned coder, but you still know your ****. thanks

    <edit> dangit, vB censored swearing. the **** above should say $ |-| | +. i guess that's how you write it without actually saying the word...</edit>
    liberate tutamet ex inferis

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    164

    Another way

    You could use this code too:

    PHP Code:

    long start
    =-1len=0;

    for(
    int i=0;i<maxStrLen;i++)
    {
     
    //Find start of string
     
    if(!isspace(userInput[i]))
     {
       
    start i;
       break;
     }
    }
    if(
    start != -1)
    {
     for(
    int i=strlen(userInput);i>0;i--)
     {
      
    //Find end of string
      
    if(!isspace(userInput[i]))
      {
        
    len start 1;
        break;
      }
     }

     
    //Move the text to the beginning of the buffer
     
    memcpy(userInput, &userInput[start], len);
    }

    //Put in terminator
    userInput[len] = 0
    Then you don't have to use a second buffer.

  5. #5
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    correct me if im wrong Gliptic but isn't memcpy() undefined behaviour if start and destination overlap as they almost certainly will do here.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,673
    > but isn't memcpy() undefined behaviour if start and destination overlap
    Yes this is correct
    Use memmove if you want to copy overlapping memory blocks.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-29-2009, 10:13 AM
  2. how to read strings?
    By happyclown in forum C Programming
    Replies: 10
    Last Post: 01-23-2009, 04:06 AM
  3. Passing strings to functions and modifying them
    By diddy02 in forum C Programming
    Replies: 6
    Last Post: 08-11-2008, 01:07 AM
  4. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 02:24 PM
  5. Table mapping Strings to Strings
    By johnmcg in forum C Programming
    Replies: 4
    Last Post: 09-05-2003, 11:04 AM

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