How to remove all spaces and tabs from a given string in C language?

旧城冷巷雨未停 提交于 2019-11-29 15:00:43

问题


What C function, if any, removes all preceding spaces and tabs from a string?

Thanks.


回答1:


In C a string is identified by a pointer, such as char *str, or possibly an array. Either way, we can declare our own pointer that will point to the start of the string:

char *c = str;

Then we can make our pointer move past any space-like characters:

while (isspace(*c))
    ++c;

That will move the pointer forwards until it is not pointing to a space, i.e. after any leading spaces or tabs. This leaves the original string unmodified - we've just changed the location our pointer c is pointing at.

You will need this include to get isspace:

#include <ctype.h>

Or if you are happy to define your own idea of what is a whitespace character, you can just write an expression:

while ((*c == ' ') || (*c == '\t'))
    ++c;



回答2:


void trim(const char* src, char* buff, const unsigned int sizeBuff)
{
    if(sizeBuff < 1)
    return;

    const char* current = src;
    unsigned int i = 0;
    while(current != '\0' && i < sizeBuff-1)
    {
        if(*current != ' ' && *current != '\t')
            buff[i++] = *current; 
        ++current;
    }
    buff[i] = '\0';
}

You just need to give buff enough space.




回答3:


A simpler function to trim white spaces

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

char * trim(char * buff);

int main()
{
    char buff[29];
    strcpy(buff, "    \r\n\t     abcde    \r\t\n     ");
    char* out = trim(buff);
    printf(">>>>%s<<<<\n",out);
}

char * trim(char * buff)
{
    //PRECEDING CHARACTERS
    int x = 0;
    while(1==1)
    {
        if((*buff == ' ') || (*buff == '\t') || (*buff == '\r') || (*buff == '\n'))
            { 
                x++;
                ++buff;
            }
        else
            break;
    }
    printf("PRECEDING spaces : %d\n",x);
    //TRAILING CHARACTERS
    int y = strlen(buff)-1;
    while(1==1)
    {
        if(buff[y] == ' ' || (buff[y] == '\t') || (buff[y] == '\r') || (buff[y] == '\n'))
            { 
                y--;
            }
        else
            break;
    }
    y = strlen(buff)-y;
    printf("TRAILING spaces : %d\n",y);
    buff[strlen(buff)-y+1]='\0';
    return buff;
}



回答4:


You can setup a counter to count the corresponding number of spaces, and accordingly shift the characters by that many spaces. Complexity for this ends up at O(n).

void removeSpaces(char *str) {
    // To keep track of non-space character count
    int count = 0;

    // Traverse the given string. If current character
    // is not space, then place it at index 'count++'
    for (int i = 0; str[i]; i++)
        if (str[i] != ' ')
            str[count++] = str[i]; // here count is
                               // incremented
    str[count] = '\0';
}


来源:https://stackoverflow.com/questions/1514660/how-to-remove-all-spaces-and-tabs-from-a-given-string-in-c-language

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!