Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   String problem (http://www.go4expert.com/forums/string-problem-t13255/)

gaurang2984 25Aug2008 17:16

String problem
 
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....

xpi0t0s 25Aug2008 23:10

Re: String problem
 
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.

ungalnanban 25Feb2010 11:22

Re: String problem
 
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.

virxen 25Feb2010 12:52

Re: String problem
 
i have written 3 general functions regarding trims

check this
Code:

#include <stdio.h>
#include <string.h>
#include <malloc.h>


void trimLeft(char [],char);
void trimRight(char [],char);
void trim(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;
}


abubacker1 25Feb2010 17:05

Re: String problem
 
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");

}


ungalnanban 25Feb2010 17:29

Re: String problem
 
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--;
        }
        string[len+1]='\0';  // 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.


All times are GMT +5.5. The time now is 22:52.