3

In my C program, I create a char array like char read_str[MAX_ALLOWED_BUFFER];. With this array, how can I trim the whitespace and newline characters from the left and right side of it. This isn't a pointer, so I don't understand how to get rid of the trailing characters. Can anyone help please?

char read_str[MAX_ALLOWED_BUFFER];
FILE *fp;
fp = fopen(argv[1],"r");
fgets(read_str, MAX_BUFFER, fp);
// how to trim read_str ?
omega
  • 40,311
  • 81
  • 251
  • 474

3 Answers3

4

You need to identify the first non-whitespace character, and then copy the values from there to the start of the array. You then need to to identify the last non-whitespace character, and create a new null-terminator character after it.

Note that you're not actually deleting or removing anything, you're simply changing the values of characters in your array.

You may find some of the string-handling functions from the C standard library useful; here is a list of them: http://en.cppreference.com/w/c/string/byte.

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
3

To trim the white space from the end, just write a 0 in the appropriate place. The string handling functions interpret that as the end of the string. To remove the white space from the beginning, the easiest method is to use a pointer into the array and let that point to the first non-whitespace character. But you could also move the characters to the start of the array, e.g. with memmove.

Daniel Fischer
  • 181,706
  • 17
  • 308
  • 431
  • How does the pointer work for a char[] type? If I get a char[], how can I get a pointer from that, then change the value of the pointer using something like pt++, then re-assign the pointer to the char[] variable? I'm a bit confused here... (i also modified my first post) – omega Jan 13 '13 at 03:04
  • I meant use a `char*`, something like `char *str_ptr = &read_str[k];`, and then pass `str_ptr` instead of `read_str`. Then you don't need to move. If that's not a good thing to do, for whatever reason, then you have to move the `char`s. – Daniel Fischer Jan 13 '13 at 03:08
  • Im still confused here, whats k supposed to be? – omega Jan 13 '13 at 03:13
  • `k` is the index of the first non-whitespace character, the place where the part of the string you're interested in starts. – Daniel Fischer Jan 13 '13 at 03:14
3

One way would be to allocate a second string and process the first into the second...

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

#define MAX_ALLOWED_BUFFER 100

char read_str[MAX_ALLOWED_BUFFER] = "    a core string     ";
char result[MAX_ALLOWED_BUFFER];

int main(void) {
  char *t;

  read_str[MAX_ALLOWED_BUFFER - 1] = '\0'; // not needed here but wise in general
  // trim trailing space
  for(t = read_str + strlen(read_str); --t >= read_str; )
    if (*t == ' ')
      *t = '\0';
    else
      break;
  // trim leading space
  for(t = read_str; t < read_str + MAX_ALLOWED_BUFFER; ++t)
    if(*t != ' ')
      break;
  strcpy(result, t);
  printf("got <%s>\n", result);
  return 0;
}
DigitalRoss
  • 143,651
  • 25
  • 248
  • 329