c - C中的字符串解析。将字符串的一部分复制到另一个字符串

标签 c string text-parsing arrays

我正在尝试编写一个函数,将字符串中的所有转义序列转换为不可打印的形式。基本上,如果我有一个字符串“This\n makes a new line”,我希望它是“This 换一条线”。到目前为止,我已经知道了。我从 main 调用:

int main()
{
    unescape("This \\n\\n is \\t\\t\\t string number \\t 7.");
    return 0;
}

char* unescape(char* s)
{
    char *esc[2] = {"\\n", "\\t"};
    int i;
    char* uus = (char*)calloc(80, sizeof(char));
    char* uus2 = (char*)calloc(80,sizeof(char));

    strncpy(uus, s, strlen(s));

    for(i = 0; i < 2; i++)
    {
        while(strstr(uus, esc[i]) != NULL) //checks if \\n can be found
        {
            //printf("\n\n%p\n\n", strstr(uus, esc[i]));
            int c = strstr(uus, esc[i]) - uus; //gets the difference between the address of the beginning of the string and the location
                                           //where the searchable string was found
            uus2 = strncpy(uus2, uus, c); //copies the beginning of the string to a new string

            //add check which esc is being used
            strcat(uus2, "\n"); //adds the non-printable form of the escape sequence
            printf("%s", uus2);

            //should clear the string uus before writing uus2 to it 
            strncpy(uus, uus2, strlen(uus2)); //copies the string uus2 to uus so it can be checked again
        }
    }
    //this should return something in the end. 
}

基本上,我现在需要做的是从“\n”之后的字符串 uus 中取出部分并将其添加到字符串 uus2 中,这样我就可以再次运行 while 循环。我考虑过使用 strtok 但碰壁了,因为它使用某种定界符制作了两个单独的字符串,而这种定界符在我的情况下并不总是存在。

编辑:将字符串的其余部分添加到 uus2 应该在 strncpy 之前。这是没有它的代码。

edit vol2:这是有效的代码,我最终使用了它。基本上编辑了 Ruud 的版本,因为我必须使用的函数必须返回一个字符串。多谢。

char* unescape(char* s)
{
    char *uus = (char*) calloc(80, sizeof(char));
    int i = 0;

    while (*s != '\0')
    {
        char c = *s++;
            if (c == '\\' && *s != '\0')
            {
                c = *s++;
                switch (c)
                {
                case 'n': c = '\n'; break;
                case 't': c = '\t'; break;
                }
            }
        uus[i] = c;
        i++;
    }
    uus[i] = '\0';
    return uus;
}

最佳答案

我同意 Anonymouse 的观点。首先替换所有 \n,然后替换所有 \t 既笨拙又低效。取而代之的是,单次遍历字符串,同时替换所有转义字符。

我在下面的代码示例中省略了空间分配;恕我直言,这是一个单独的责任,不是算法的一部分,因此不属于同一功能。

void unescape(char *target, const char *source)
{
    while (*source != '\0')
    {
        char c = *source++;
        if (c == '\\' && *source != '\0')
        {
            c = *source++;
            switch (c)
            {
                case 'n': c = '\n'; break;
                case 't': c = '\t'; break;
            }
        }
        *target++ = c;
    }
    *target = '\0';
}

编辑: 这是一个替代版本,使用 Anonymouse 建议的 strchr。 此实现应该更快,尤其是在转义字符相对较少的非常长的字符串上。 我发布它主要是为了演示优化如何使您的代码更复杂且可读性更差;因此更难维护且更容易出错。有关详细讨论,请参阅:http://c2.com/cgi/wiki?OptimizeLater

void unescape(char *target, const char *source)
{
    while (*source != '\0')
    {
        if (*source++ == '\\' && *source != '\0')
        {
            char c = *source++;
            switch (c)
            {
                case 'n': c = '\n'; break;
                case 't': c = '\t'; break;
            }
            *target++ = c;
        }
        else
        {
            const char *escape = strchr(source--, '\\');
            int numberOfChars = escape != NULL ? escape - source : strlen(source);
            strncpy(target, source, numberOfChars);
            target += numberOfChars;
            source += numberOfChars;
        }
    }
    *target = '\0';
}

关于c - C中的字符串解析。将字符串的一部分复制到另一个字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23032996/

相关文章:

c - Eclipse SDK 程序 "cl"在 PATH 错误中找不到

c - 在带有 busybox 的嵌入式 linux 上使用 system() 时,SIGHUP 信号处理程序重置为默认值

c++ - 是否可以为 std::string 和 std::wstring 编写一个函数?

java - 尝试分割长字符串并将其数据用于列表

python - 使用 python 生成字符串中包含的数值的索引

regex - 在 R 中列出 HTTP/FTP 服务器上的文件

csv - Google Apps 脚本 txt 文件 getDataAsString() 返回字符之间的 � 和方形符号

c - 递增数组指针

将 char[2] 转换为 uint16_t 以进行套接字编程?

c - C中的memcmp、strcmp和strncmp有什么区别?