繁体   English   中英

在C中删除某个字符后的字符串结尾

[英]Removing the end of a string after a certain character in C

我正在尝试在C中某个字符之后结束字符串。此程序将与文件系统一起使用,因此将重复该字符,我需要找到该字符的最后一次出现,然后删除此后的所有内容。

我从互联网上找到了一些东西,但是那不起作用,我也不知道为什么。

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

void deleteEnd (char* myStr){

    printf ("%s\n", myStr);
    char *del = &myStr[strlen(myStr)];

    while (del > myStr && *del != '/')
        del--;

    if (*del== '/')
        *del= '\0'; // the program crashes here

    return;
}

int main ( void )
{

    char* foo= "/one/two/three/two";
    deleteEnd(foo);
    printf ("%s\n", foo);

    return 0;
}

这段代码基本上会找到最后一个'/'字符,并将空终止符放在那里。 它在理论上有效,但在实践中却不可行。

顺便说一句,如果我的方法是错误的,是否有更好的方法来做到这一点?

谢谢。

**编辑:根据建议,我用“ strrchr()”替换了我的代码,但仍然没有结果:

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

void deleteEnd (char* myStr){

    char *lastslash;

    if (lastslash = strrchr(myStr, '/'))
        *lastslash = '\0'; // the code still crashes here.

    return;
}

int main ( void )
{

    char* foo= "/one/two/three/two";
    deleteEnd(foo);
    printf ("%s\n", foo);

    return 0;
}

在C中,当您编写像这样的文字字符串时:char * foo =“” / one / two / three / two“;

它们是不可变的,这意味着它们被嵌入在可执行文件中并且是只读的。

尝试修改只读数据时会遇到访问冲突(崩溃)。

相反,您可以将字符串声明为chars数组,而不是文字字符串。

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

void deleteEnd (char* myStr){

    printf ("%s\n", myStr);
    char *del = &myStr[strlen(myStr)];

    while (del > myStr && *del != '/')
        del--;

    if (*del== '/')
        *del= '\0';

    return;
}

int main ( void )
{

    char foo[] = "/one/two/three/two";
    deleteEnd(foo);
    printf ("%s\n", foo);

    return 0;
}

yeschar *最后一个斜杠;

if (lastslash = strrchr(myStr, '/'))
    *lastslash = '\0'; // the code still crashes here.

return;

}

int main(void){

char foo[]= "/one/two/three/two";
deleteEnd(foo);
printf ("%s\n", foo);
  1. 从第一个字符开始。
  2. 扫描当前字符,是/? 没有? 转到步骤3。是吗? 转到步骤4。
  3. 还有另一个角色吗? 没有? 转到步骤5。是吗? 转到下一个字符,然后转到步骤2。
  4. 将“ lastSlash”变量设置为当前字符位置。 转到步骤2。
  5. 删除lastSlash位置之后的所有内容。 你完成了!

只需将foo声明为chars数组,而不是指向char的指针即可:

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

void deleteEnd (char* myStr){

    char *lastslash;

    if (lastslash = strrchr(myStr, '/'))
        *lastslash = '\0'; // the code still crashes here.

    return;
}

int main ( void )
{

    char foo[]= "/one/two/three/two";
    deleteEnd(foo);
    printf ("%s\n", foo);

    return 0;
}

您不能更改字符串常量,必须将其复制到新的char字符串。

#include <iostream>
#include <cstring>

void delete_end(char* dest, const char* source)
{
    const char* p = source + strlen(source) - 1;
    while(*p != '/' && p > source)
        --p;
    strncpy(dest, source, p - source);
    dest[p-source] = '\0';
}

int main()
{

    const char* str = "one/two/three/four";
    char buf[256];
    delete_end(buf, str);
    std::cout << buf << std::endl;

    return 0;
}

要找到C中字符串中字符的最后一次出现,请使用strrchr(3)函数。 其原型位于<string.h>中

暂无
暂无

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

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