Home
Questions
Search
Forum
Contact
Guest Book
Polls!
Got a Question?
 
PREV
Programs
NEXT
 
(86 / 301)
 
 



Write your own trim() or squeeze() function to remove the spaces from a string.




Here is one version...


#include <stdio.h>

char *trim(char *s);

int main(int argc, char *argv[])
{
  char str1[]=" Hello   I am Good ";
  printf("\n\nBefore trimming : [%s]", str1);
  printf("\n\nAfter trimming  : [%s]", trim(str1));
  
  getch();
}


// The trim() function...

char *trim(char *s) 

    char *p, *ps; 
    
    for (ps = p = s; *s != '\0'; s++) 
    {
        if (!isspace(*s)) 
        {
            *p++ = *s; 
        }
    }

    *p = '\0'; 

    return(ps); 
}


And here is the output...


Before trimming : [ Hello   I am Good ]
After trimming  : [HelloIamGood]



Another version of this question requires one to reduce multiple spaces, tabs etc to single spaces...


PREV
COMMENTS                                  INDEX                                  PRINT
NEXT



Last updated: November 3, 2005

www.cracktheinterview.com - Your destination for the most common IT interview questions, answers, frequently asked interview questions (FAQ), C Programs, C Datastructures for technical interviews conducted by the top IT companies around the world!