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

发布于 2024-08-06 02:50:06 字数 40 浏览 5 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

萌辣 2024-08-13 02:50:06

在 C 中,字符串由指针标识,例如 char *str,也可能是数组。无论哪种方式,我们都可以声明我们自己的指针,该指针将指向字符串的开头:

char *c = str;

然后我们可以使指针移动经过任何类似空格的字符:

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

这将使指针向前移动,直到它不指向空格,即之后任何前导空格或制表符。这使得原始字符串保持不变 - 我们刚刚更改了指针 c 指向的位置。

您将需要此包含来获取 isspace

#include <ctype.h>

或者,如果您愿意定义自己对空白字符的想法,您可以编写一个表达式:

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

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;
哑剧 2024-08-13 02:50:06

一个更简单的修剪空白的函数

#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;
}

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;
}
生生漫 2024-08-13 02:50:06
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';
}

你只需要给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.

猫弦 2024-08-13 02:50:06

您可以设置一个计数器来计算相应的空格数,并相应地将字符移动相应的空格数。其复杂度最终为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';
}

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]; // increment count
    str[count] = '\0';
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文