简体   繁体   中英

How to delete last part of string in C

I need a certain part of the string for example I have

folder1/folder2/folder3/folder4

and I don't want folder4 in my new string so it should be like folder1/folder2/folder3

any help would be appreciated I searched for strtok and strchar but I couldn't figure out how to achieve this

Find the last occurance of / using strrchr() and then replace it with \\0 .

int main()
{
    char string[] = "folder1/folder2/folder3/folder4";
    char character = '/'; 
    char* ptr = strrchr(string, character);
    *ptr = '\0';
    cout<<string;
    return 0;
}

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