1. int trim(char* c){ 2. char * i; 3. if(c==0 || !strlen(c)) 4. return 0; 5. for(i=c ; isspace(*i)!=0 && (i-c)<strlen(c); i++); 6. memmove(c, i, strlen(c)-(i-c)+1); 7. if(!strlen(c)) 8. return 0; 9. for(i=(c+strlen(c)-1) ; isspace(*i)!=0 && i>c; i--); 10. *(i+1)='\0'; 11. return strlen(c); 12. } 13. 14. int main(int vv, char** cc){ 15. char xman[] =" \t String that needs trimming \n "; 16. trim(xman); 17. printf ("--%s--\n", xman); 18. } |
Programming tutorials, examples and code snippets. C++, Java, Perl, PHP and javascript. Enterprise examples using javaEE, JNDI and Beans. Core tutorials using sockets, threads, database, IPC, XML etc. Web 2 widgets using javascrpt. Simple easy to understand step by step tutorials,
C/C++ Trimming whitespace - Trim
How to trim strings in C/C++ is a commonly asked question. Below is a simple C/C++ function that will trim leading and trailing whitespaces. Whitespaces include spaces, tabs, carriage return, new lines etc. It get's rid of 'white' spaces at the beginning and at the end of an array of characters.
Subscribe to:
Posts (Atom)
