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....
|
Mentor
|
![]() |
| 25Aug2008,23:10 | #2 |
|
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. |
|
Go4Expert Member
|
|
| 25Feb2010,11:22 | #3 |
|
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");
}
Code:
welcome to go4Expert ======= welcome to go4Exper======= Last edited by ungalnanban; 25Feb2010 at 11:23.. Reason: making correct align ment. |
|
Pro contributor
|
![]() |
| 25Feb2010,12:52 | #4 |
|
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;
}
|
|
Light Poster
|
|
| 25Feb2010,17:05 | #5 |
|
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");
}
|
|
Go4Expert Member
|
|
| 25Feb2010,17:29 | #6 |
|
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");
}
Code: Code:
welcome to go4Expert ======= welcome to go4Expert======= Note: Last post for This node I did a mistake in coding. Now I correct it and I highlight that in code. Last edited by ungalnanban; 25Feb2010 at 17:30.. Reason: alignlemt changes |


