简体   繁体   English

C语言从给定字符串中删除前面的空格和制表符

[英]Remove preceding spaces and tabs from a given string in C language

哪个 C 函数(如果有)从字符串中删除所有前面的空格和制表符?

In C a string is identified by a pointer, such as char *str , or possibly an array.在 C 中,字符串由指针标识,例如char *str ,或者可能是数组。 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, ie after any leading spaces or tabs.这将向前移动指针,直到它不指向空格,即在任何前导空格或制表符之后。 This leaves the original string unmodified - we've just changed the location our pointer c is pointing at.这使原始字符串保持不变——我们只是改变了指针c指向的位置。

You will need this include to get isspace :您将需要此包含来获取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;

A simpler function to trim white spaces修剪空白的更简单的功能

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

char * trim(char * buff);

int main()
{
    char 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;
}
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.你只需要给buff足够的空间。

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) .其复杂性最终为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]; // increment count
    str[count] = '\0';
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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