Home > Blockchain >  Removing a newline from the middle of a string using getchar
Removing a newline from the middle of a string using getchar

Time:11-26

Right now I have a string looking like this:

A sentence        
with a newline.

I'm reading the string in via console input like so:

ch = getchar();

while (ch != '.') {
  msg[i] = ch;
  i  ;
  ch = getchar();
}

And, after reading it in, I remove the whitespace present by doing this in a function and applying to the msg char array:

char *remove_white_spaces(char *str) {
    int i = 0, j = 0;
    while (str[i]) {
        if (str[i] != ' ')
      str[j  ] = str[i];
        i  ;
    }
    str[j] = '\0';
    return str;
}

I've tried looping over it and stopping at \n but that leaves an output of "Asentence", as the string terminates as the \n is set to 0.

Whole main:

int main(void) {
  char msg[MAX_MSG_LEN 1];
  char ch;
  int i = 0;
  ch = getchar();

  while (ch != '.') {
    msg[i] = ch;
    i  ;
    ch = getchar();
  }

  msg[i] = '.';
  msg[i   1] = '\0';
  remove_white_spaces(msg);

  printf("%s\n", msg);
  return 0;
}

CodePudding user response:

You can use the isspace function to test for and skip any/all whitespace characters, include the normal space and the newline character(s):

#include <ctype.h> // For definition of "isspace"

char *remove_white_spaces(char *str) {
    int i = 0, j = 0;
    while (str[i]) {
        if (!isspace((unsigned char)(str[i])))
            str[j  ] = str[i];
        i  ;
    }
    str[j] = '\0';
    return str;
}

On the reason for casting the argument to isspace to an unsigned char, see this discussion.

CodePudding user response:

Function removing and replacing any chars in the string.

  • toRemove - chars to remove
  • addSpace - replace with space
  • allowMultiple - allow multiple spaces when replacing more adjanced
    characters
  • allowEdges - allow adding spaces at the from and at the end
char *removeChars(char *str, const char *toRemove, const int addSpace, const int allowMultiple, int const allowEdges)
{
    char *rd = str, *wr = str;
    int replaced = 0;
    if(rd)
    {
        while(*rd)
        {
            if(strchr(toRemove, *rd))
            {
                if(addSpace)
                {
                    if(!replaced || allowMultiple)
                    {
                        if(wr != str || (wr == str && allowEdges))
                        {
                           *wr   = ' ';
                           replaced = 1;
                        }
                    }
                }
            }
            else 
            {
                *wr   = *rd;
                replaced = 0;
            }
            rd  ;
        }
        if(allowEdges) *wr = 0;
        else 
        while((wr - 1) > str) 
        {
            if(*(wr - 1) == ' ') {*(wr - 1) = 0; wr--;}
            else break;
        }
    }
    return str;
}

int main(void)
{
    char str[] = "%%%%%A sentence\n\n\nwith!@#$%^a newline.%%%%%%%";

    printf("`%s`\n", removeChars(str,"\n!@#$%^", 1, 0, 0));
}

CodePudding user response:

Following the suggestion of @MarkBenningfield I did the following and checked for '\n' and just replaced it with a space.

while (ch != '.') {
  msg[i] = ch;
  i  ;
  ch = getchar();

  if (ch == '\n') {
    ch = ' ';
  }
}
  • Related