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

时间:2022-08-22 11:59:08

I'm trying to end my string after a certain character in C. This program will work with the file system so the character will be repeated, I need the find the last occurence of that character end delete everything after that.

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

I found something from the internet but that doesn't work and I don't know why.

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

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

this code basically finds the last '/' character and places the null terminator there. It works theorically but not practically.

这段代码基本上找到了最后一个'/'字符并将null终止符放在那里。它在理论上有效但不实用。

By the way if my way is wrong, is there any better way to do this?

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

Thank you.

**edit: I replaced my code with "strrchr()" upon the suggestions but still got no result:

**编辑:我根据建议用“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;
}

6 个解决方案

#1


2  

In C, When you write literal strings like this: char* foo= "/one/two/three/two";

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

Those are immutable, which means they are embedded in the executable and are read only.

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

You get an access violation (crash) when trying to modify read only data.

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

Instead you can declare your string as an array of chars instead of a literal string.

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

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

#2


1  

yeschar *lastslash;

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

return;

}

int main ( void ) {

int main(void){

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

#3


0  

  1. Start at the first character.
  2. 从第一个角色开始。

  3. Scan the current character, is it /? No? Go to step 3. Yes? Go to step 4.
  4. 扫描当前字符,是吗?没有?转到第3步。是吗?转到第4步。

  5. Is there another character? No? Go to step 5. Yes? Go to the next character and go to step 2.
  6. 还有另一个角色吗?没有?转到第5步。是吗?转到下一个字符,然后转到第2步。

  7. Set a "lastSlash" variable to current character position. Go to step 2.
  8. 将“lastSlash”变量设置为当前字符位置。转到第2步。

  9. Delete everything after lastSlash's position. You're done!
  10. 删除lastSlash位置后的所有内容。你完成了!

#4


0  

Just declare foo as an array of chars, instead of a pointer to a char:

只需将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;
}

#5


0  

You can't change in a string constant, you must copy to a new char string.

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

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

#6


0  

to find the last occurrence of a character in a string in C, use the strrchr(3) function. Its prototype is in <string.h>.

要在C中查找字符串中最后一个字符,请使用strrchr(3)函数。它的原型在 中。

#1


2  

In C, When you write literal strings like this: char* foo= "/one/two/three/two";

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

Those are immutable, which means they are embedded in the executable and are read only.

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

You get an access violation (crash) when trying to modify read only data.

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

Instead you can declare your string as an array of chars instead of a literal string.

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

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

#2


1  

yeschar *lastslash;

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

return;

}

int main ( void ) {

int main(void){

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

#3


0  

  1. Start at the first character.
  2. 从第一个角色开始。

  3. Scan the current character, is it /? No? Go to step 3. Yes? Go to step 4.
  4. 扫描当前字符,是吗?没有?转到第3步。是吗?转到第4步。

  5. Is there another character? No? Go to step 5. Yes? Go to the next character and go to step 2.
  6. 还有另一个角色吗?没有?转到第5步。是吗?转到下一个字符,然后转到第2步。

  7. Set a "lastSlash" variable to current character position. Go to step 2.
  8. 将“lastSlash”变量设置为当前字符位置。转到第2步。

  9. Delete everything after lastSlash's position. You're done!
  10. 删除lastSlash位置后的所有内容。你完成了!

#4


0  

Just declare foo as an array of chars, instead of a pointer to a char:

只需将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;
}

#5


0  

You can't change in a string constant, you must copy to a new char string.

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

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

#6


0  

to find the last occurrence of a character in a string in C, use the strrchr(3) function. Its prototype is in <string.h>.

要在C中查找字符串中最后一个字符,请使用strrchr(3)函数。它的原型在 中。