Hi all, I want remove tail end whitespace from string in c.for example- i/p string is "today is monday "and o/p string is "today is monday" plz help asap. thanks in advance....
It's not difficult. Where are you stuck? Generally the algorithm for this would be: find the last character in the string if it's a space, replace it with \0 repeat until the last character in the string isn't a space.
See the following Example C program to remove the training space. Code: #include<stdio.h> #include<string.h> main() { char string[]="welcome to go4Expert "; int len=strlen(string); // find the length of the string printf("%s",string); printf("=======\n"); //indicator. int c; int i=0; len=len-1; while(isspace((c=string[len]))) // read the string from end up to find a character. { len--; } string[len]='\0'; // insert the null character after found the alpha character printf("%s",string); printf("=======\n"); } Output: Code: welcome to go4Expert ======= welcome to go4Exper======= in first line the indicator prints after some space. in second line the indicator prints near the string.
i have written 3 general functions regarding trims check this Code: #include <stdio.h> #include <string.h> #include <malloc.h> void [COLOR=Blue]trimLeft[/COLOR](char [],char); void [COLOR=Blue]trimRight[/COLOR](char [],char); void [COLOR=Blue]trim[/COLOR](char[],char); void trimRight(char string[],char what){//what=the char to remove int len= strlen(string)-1; while (string[len-1]==what){ len--; } string[len]= '\0'; } void trimLeft(char string[],char what){ int i=0,len=0,len1=0,size=0; len=strlen(string); while (string[len1]==what){ len1++; } size=len-len1; char * rest=NULL; if (len1>0){ rest=(char *)malloc((size+1)*sizeof(char)); for (i=len1;i<len;i++){ rest[i-len1]=string[i]; } rest[size]='\0'; strcpy(string,rest); } } void trim(char string[],char what){ trimRight(string,what); trimLeft(string,what); } int main(){ char test1[]="+++++++++++++++test1+++++++++++++++"; printf("\nbefore trimRight:<%s>",test1); trimRight(test1,'+'); printf("\nafter trimRight:<%s>",test1); printf("\n"); char test2[]="@@@@@@@@@@@@test2------------"; printf("\nbefore trimLeft:<%s>",test2); trimLeft(test2,'@'); printf("\nafter trimLeft:<%s>",test2); printf("\n"); char test3[]="--------------test3------------"; printf("\nbefore trim:<%s>",test3); trim(test3,'-'); printf("\nafter trim:<%s>",test3); printf("\n"); getchar(); return 0; }
Hope this is a better way Code: #include<stdio.h> #include<string.h> #include<malloc.h> main() { char *str ; char string[]="welcome to go4Expert "; int len=strlen(string); // find the length of the string printf("%s",string); printf("=======\n"); //indicator. int c; int i=0; len--; str = string ; str+=len ; while (isspace((*str--))); *(str+2)='\0'; // insert a null character printf("%s",string); printf("=======\n"); }
See the following Example C program to remove the training space. Code: Code: #include<stdio.h> #include<string.h> main() { char string[]="welcome to go4Expert "; int len=strlen(string); // find the length of the string printf("%s",string); printf("=======\n"); //indicator. int c; int i=0; len=len-1; while(isspace((c=string[len]))) // read the string from end up to find a character. { len--; } [B] string[len+1]='\0'; [/B]// insert the null character after found the alpha character printf("%s",string); printf("=======\n"); } Output: Code: Code: welcome to go4Expert ======= welcome to go4Expert======= in first line the indicator prints after some space. in second line the indicator prints near the string. Note: Last post for This node I did a mistake in coding. Now I correct it and I highlight that in code.