在 C 中删除字符串的某些字符

问题描述 投票:0回答:0

我一直试图从字符串中删除一些字符(一个在特定位置,另一个在末尾),但没有成功。我已经阅读了很多关于 stackoverflow 的帖子,因为这是一个已经回答过的热门问题,我是这样认为的:Removing last character in C

但是到目前为止我还没有成功:

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

int main()
{
                // I just want a string of [email protected] in the end
                // so I performed all the steps below:

                const char c = '<';
                char *ret = strchr("FROM:<[email protected]>", c);
                char *stripFrontBac = ret;
                
                
                printf("value of ret %s\n", stripFrontBac);
             
                // this part here removes the Front of the current string stripFrontBac
                stripFrontBac++;
                printf("value of ret %s\n", stripFrontBac);
                
                // here I tried to remove the last character like
                // how it was done in the attached link. But
                // error must have happened in this line. Since
                // when I step into the following in a debugger, it says "seg fault"
                stripFrontBac[strlen(stripFrontBac)-1] = '\0';
                
                // this line still has the symbol > in the end of the string
                printf("value of ret %ld\n", strlen(stripFrontBac));

}

有人能告诉我我可能做错了什么吗?

我实际上已经尝试过使用调试器,它说存在“seg fault”。 我已经花了几个小时在这上面。请不要只是否定我的帖子,我已经尝试在 stackoverflow 上搜索帖子并尝试找出原因,但还没有成功。

谢谢

arrays c c-strings
© www.soinside.com 2019 - 2024. All rights reserved.