1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

String problem

Discussion in 'C' started by gaurang2984, Aug 25, 2008.

  1. gaurang2984

    gaurang2984 New Member

    Joined:
    Sep 5, 2007
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    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....
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    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.
     
  3. ungalnanban

    ungalnanban New Member

    Joined:
    Feb 19, 2010
    Messages:
    45
    Likes Received:
    2
    Trophy Points:
    0
    Location:
    Chennai
    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.
     
    Last edited: Feb 25, 2010
  4. virxen

    virxen New Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    0
    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;
    }
    
    
    
     
  5. abubacker1

    abubacker1 New Member

    Joined:
    Feb 20, 2010
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    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");
    
    }
    
     
  6. ungalnanban

    ungalnanban New Member

    Joined:
    Feb 19, 2010
    Messages:
    45
    Likes Received:
    2
    Trophy Points:
    0
    Location:
    Chennai
    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.
     
    Last edited: Feb 25, 2010

Share This Page