简体   繁体   中英

Function and strings

I'm new to C and have started learning about strings. I want to create a function called

void SpaceRemover(char *input )

It should remove the spaced from a given string array that has lots of space

The code that I've produced so far removes all the spaces and doesn't provide the output I'm looking for. Can anyone help me with this?

char* SpaceRemover(char *input){
    char *output=input;
    for (int i = 0, j = 0; i<strlen(input); i++,j++)
    {
        if (input[i]!=' ')   
            output[j]=input[i]; 
        else
            j--;   
     }
     return output;
}

I made this but I know its wrong and does not do what i want it to but honestly this is all i could this of -_-

The problem is that you remove all spaces.

char *SpaceRemover(char *name){
  char *output = name;
  int j = 0;

  for (int i = 0; i < strlen(name); i++) {
    if (name[i] != ' ' || (name[i] == ' ' && name[i + 1] != ' ') {  
      output[j] = name[i];
      j += 1; 
    }
  }
  output[j] = '\0'
  return output;
  }

This condition should let one space through.
You might notice I replaced the void return type with a char * so to use the function you will need to use:

name = SpaceRemover(name);

You could just do as below

void SpaceRemover(char *name)
{
  int i=0,j=0;

  for (i = 0;i<strlen(name);i++)
   {
           if (name[i] != ' ' || (name[i] == ' ' && name[i+1] != ' ' && j!= 0))
           {
                   name[j++] = name[i];
           }
  }
  name[j]='\0'; //Terminate the string to avoid junk chars 
}

Where

if (name[i] != ' ' || (name[i] == ' ' && name[i+1] != ' ' && j != 0))

will let you copy only if current char is not space or current char is space and next char is not space(to include single space apart in the beginning).

Also don't forget to terminate the string .

name[j]='\0';

I would be using a flag to activate when a space is met.

This might need to be tweaked if you want to remove leading and trailing spaces too.

A space will be added to output and the flag will be used to avoid the next ones to be added. The flag will be deactivated when something else than a space is met.

As stated Alex in comments, decrementing j in loop while it's incremented in the for statement isn't recommended.

I would copy each characters in the for loop instead of filtering a pre-copied output.

char space_found = 0;
char *output = malloc(sizeof(char) * (strlen(name) + 1));
int j = 0;
for (int i = 0; i < strlen(name); ++i)
{
    if (name[i] == ' ' and space_found == 0)
    {
        space_found = 1;
        output[j++] = name[i];
    }
    if (name[i] != ' ')
    {
        space_found = 0;
        output[j++] = name[i];
    }
}
output[j] = '\0';
void spaceRemover(char* str)
{
    char temp[50] = {0};
    int j = 0; 
    strncpy(temp, str, strlen(str) + 1);
    for(int i = 0; i < strlen(str); i++)
    {
        if(temp[i] != ' ')
        {
            str[j] = temp[i];
            j++;
        }   
    }
    str[j] = 0;
}

if you have any questions, feel free to ask, Good Luck

#include <stdio.h>

char *rem(char *str)
{
   char *cur=str;
   char *nex=str;

   while(*nex)
   {
      if(*nex == ' ') nex++;
      else *cur++ = *nex++;
   }
   *cur=0;
   return str;
}

int main(void) {
    char z[]=" etc def etc    def ";
    printf("%s\n", rem(z));
    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM