Spread the love
Write your own trim() or squeeze() function to remove the spaces from a string
Algorithm
Algorithm is very simple we just Shift each character one by one if space appear don’t shift it.
#include<stdio.h> void trim(char *s); int main() { char str[]=" www . firmcodes . com "; trim(str); printf("%s",str); return 0; } void trim(char *s) { char *trimed=s; while(*s) // Check for end of string until null character appear { *trimed=*s; //shift each character if( *(s+1)==' ' ) //Check for space if space appear jump that address ++s; if( *(s+1)=='\0' ) //look for null character if null present, terminate shifting *(trimed+1)='\0'; trimed++; s++; } }
Output
Suggested Reading
- Write a C program to reverse the words in a sentence in place
- Write a C program to reverse the string without using strrev() ?
- Write a C program which does wildcard pattern matching algorithm