Thread: Removing whitespace

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Ok, well you are trying...try this.


    Code:
    char *Trim(char *source)
    {
    
    int len = strlen(source);
    
    if(len == 0){char *error = malloc(1); return error = 0;}
    
    char *target = malloc(len);
    
    int a = 0;
    
    while(a < len)
    if(!isspace(source[a++]))
    break;
    
    int x = len;
    
    while(x > 0)
    if(!isspace(source[x--]))
    break;
    
    int i = 0;
    
    for(i = a; i <= x; i++)
    *target++= source[i];
    
    target[strlen(target)-1] = 0;
    
    return target;
    }


    But remember that because the memory for the target string is allocated within the function, (has to be), do not allocate for it in the main program:

    char string[] = " Programming is fun !!! ";

    char *trim = malloc(strlen(string));
    trim = Trim(string);
    // !...Memory Leak...! //

    char *trim = Trim(string);
    // !...Correct...! //
    Last edited by Sebastiani; 12-30-2001 at 02:09 PM.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 06-20-2006, 03:07 PM
  2. Removing leading and trailing whitespace
    By JimpsEd in forum C Programming
    Replies: 2
    Last Post: 05-14-2006, 03:55 PM
  3. Removing Leading & Trailing Whitespace
    By EvilGuru in forum C Programming
    Replies: 11
    Last Post: 12-01-2005, 02:59 PM
  4. Regaurding SetConsoleTitle() and whitespace.
    By Tronic in forum Windows Programming
    Replies: 4
    Last Post: 03-26-2004, 09:02 PM
  5. Whitespace and scanf()
    By Procyon in forum C Programming
    Replies: 1
    Last Post: 01-05-2002, 01:55 AM