I have a string that has a bunch of white space on the end of it and was wondering if there is a simple way to eliminate the trailing white space with a standard C function. I know there is one called "String.trim" but I can't use anything that isn't in the standard C library.

here is what i have, if you have a more efficient way to read the data to avoid reading the whitespace, that is more than welcome too.

fgets(char_buffer,40,file);
strcpy(my_show_array[i].show_name, char_buffer); 
printf("show name = %s\n", my_show_array[i].show_name);

Of course "show_name" is not always the same so i have to assign enough space to hold at least 40 characters if needed. however if it is not needed, I want to trim it down.

If I do a print statement,

printf("%s is my string", my_show_array[i].show_name);

Assuming my_show_array.show_name = "my string" It prints this out,

"my string                                
is my string"
/*notice I don't want to eliminate ALL white space b/c 
my_show_array[i].show_name has a space in between the two words, "my" and "string"*/

I think it's either a lot of white space or perhaps an end of line marker...


---EDIT---
I ran a strlen command and it returned something small like 8, so it must be an end of line marker b/c it always prints out on the next line in the command window when I run the program. I still don't know how to get rid of this though.

Edited 5 Years Ago by logicmonster: n/a

Get string length.
Start at the end of the string.
If character is a SPACE, back up 1 character and repeat
When the character is not a SPACE change the next char (the last SPACE seen) to \0

Get string length.
Start at the end of the string.
If character is a SPACE, back up 1 character and repeat
When the character is not a SPACE change the next char (the last SPACE seen) to \0

I tried doing this which is what I think your talking about,

fgets(char_buffer,40,file);															len=strlen(char_buffer);
for(j=len;j=0;j--)
  {
	if(char_buffer[j]!=' ')
		{
		char_buffer[j+1]='\0';
		}
  }
strcpy(my_show_array[i].show_name, char_buffer); 
printf("show name = %s\n", my_show_array[i].show_name);

I'm not 100% confident I wrote that correctly but it ended that the file was reading in an end line character from my file. I was able to fix it by getting rid of it in the file itself. Thanks for helping me think it through.

add a break statement after line 6 to stop the loop, otherwise the program will just keep on looking for spaces.

>>I'm not 100% confident I wrote that correctly
The easiest way to determine if the code you write works correctly is to run it several times using different input strings. Then in the printf() statement surround the resulting string with quotes printf("show name = \"%s\"\n", char_buffer);

If you just want to get rid of the newline char, this works great. Put it as the next line after fgets():

int len=strlen(char_buffer);
if(char_buffer[len-1]=='\n')
  char_buffer[len-1]='\0';

Your string will look like this after fgets():
my string\n\0

and what this makes the string into is just:
my string\0

Because it seems like white space wasn't the problem, but the newline char was.

This article has been dead for over six months. Start a new discussion instead.