-4

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

Ozan Mudul
  • 750
  • 1
  • 9
  • 20

1 Answers1

-1

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;
}
Himanshu Singh
  • 2,117
  • 1
  • 5
  • 15
  • 1
    This is not C++ question, it's C. And [you shouldn't include bits/stdc++.h](https://stackoverflow.com/Questions/31816095/Why-Should-I-Not-Include-Bits-Stdc-H.) – KamilCuk Dec 30 '19 at 11:05
  • @KamilCuk you are right but it doesn't effect the main context of the answer. But anyways i'll remove it. Thanks. – Himanshu Singh Dec 30 '19 at 11:07