简体   繁体   中英

Removing the end of a string after a certain character in C

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.

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.

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:

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

In C, When you write literal strings like this: 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;
}

yeschar *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);
  1. Start at the first character.
  2. Scan the current character, is it /? No? Go to step 3. Yes? Go to step 4.
  3. Is there another character? No? Go to step 5. Yes? Go to the next character and go to step 2.
  4. Set a "lastSlash" variable to current character position. Go to step 2.
  5. Delete everything after lastSlash's position. You're done!

Just declare foo as an array of chars, instead of a pointer to a 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;
}

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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