Home > Back-end >  Eject excess space from string in C
Eject excess space from string in C

Time:02-08

I need to write a function which will eject excess space from string in C.

Example:

char s[]="  abcde    abcde    "; 

OUTPUT:

"abcde abcde"

Code:

#include <stdio.h>
#include <ctype.h>
char *eject(char *str) {
  int i, x;
  for (i = x = 0; str[i];   i)
    if (!isspace(str[i]) || (i > 0 && !isspace(str[i - 1])))
      str[x  ] = str[i];
  if(x > 0 && str[x-1] == ' ') str[x-1] = '\0';
  return str;
}

int main() {
  char s[] = "  abcde    abcde    ";
  printf("\"%s\"", eject(s));
  return 0;
}

This code doesn't work for string " " If this string is found program should print:

""

How to fix this?

CodePudding user response:

You could write two functions which trim leading and trailing whitespace characters.

void trim_front(char *src) {
    size_t i = 0, j = 0;
    while (isspace(src[i])) i  ;
    while (i < strlen(src)) src[j  ] = src[i  ];
    src[j] = '\0';
}

void trim_back(char *src) {
    char *ch = src   strlen(src) - 1;
    while (isspace(*ch)) *ch-- = '\0';
}

If you know you don't have to deal with trailing or leading spaces, your task becomes much simpler.

void reduce_spaces(char *src) {
    size_t i = 0, j = 0;

    for (; i < strlen(src);   i) {
        if (i == strlen(src) - 1 || 
            (isspace(src[i]) && !isspace(src[i   1])) ||
            !isspace(src[i])) {
            src[j  ] = src[i];
        }
    }

    src[j] = '\0';
}

And testing this:

int main(void) {
    char s[] = "     hello    world     ";

    trim_front(s);
    trim_back(s);
    reduce_spaces(s);

    printf(">%s<\n", s);

    return 0;
}
% gcc test.c
% ./a.out
>hello world<
%

Of course, if you really want to, you can transplant the code from those functions into reduce_spaces, but decomposing a problem into multiple smaller problems can make things much easier.

CodePudding user response:

Basically, you need to remove the consecutive space characters between the words in the input string and all leading and trailing space characters of the input string. That means, write code to remove the consecutive space characters in the input string and while removing the consecutive space characters, remove the leading and trailing space characters completely.

You can do it in just one iteration. No need to write the different functions for removing the leading and trailing spaces of input string, as shown in the other post.

You can do:

#include <stdio.h>
#include <ctype.h>

char * eject (char *str) {
    if (str == NULL) {
        printf ("Invalid input..\n");
        return NULL;
    }

    /* Pointer to keep track of position where next character to be write 
     */
    char * p = str;
    for (unsigned int i = 0; str[i] ;   i) {
        if ((isspace (str[i])) && ((p == str) || (str[i   1] == '\0') || (str[i] == (str[i   1])))) {
            continue;
        }
        *p   = str[i];
    }

    /* Add the null terminating character. 
     */
    *p = '\0';
    return str;
}

int main (void) {
  char s[] = "  abcde    abcde    ";
  printf("\"%s\"\n", eject(s));

  char s1[] = "      ";
  printf("\"%s\"\n", eject(s1));

  char s2[] = "ab  yz   ";
  printf("\"%s\"\n", eject(s2));

  char s3[] = "  ddd xx  jj m";
  printf("\"%s\"\n", eject(s3));

  char s4[] = "";
  printf("\"%s\"\n", eject(s4));

  return 0;
}

Output:

# ./a.out
"abcde abcde"
""
"ab yz"
"ddd xx jj m"
""

CodePudding user response:

Simple and easy small WorkAround to your problem. Can be easily convert to only pointer.

char* eject(char* str) {
    int i, x;

    for (i = 0; str[i] != '\0'; i  ) {
        if (str[i] != ' ') i = 200; //If it's not a full space string, we run your loop
    }

    if (i == 200) {
        for (i = x = 0; str[i];   i)
            if (!isspace(str[i]) || (i > 0 && !isspace(str[i - 1])))
                str[x  ] = str[i];
        if (x > 0 && str[x - 1] == ' ') str[x - 1] = '\0';
    }
    else *str = '\0'; //Else str = '\0' so string returned is ""

    
    return str;
}
  •  Tags:  
  • Related