Twisterz
Twisterz

Reputation: 424

How to get the last part of a string in C

I have a string in C that contains a file path like "home/usr/wow/muchprogram".

I was wondering in C how I can get the string after the last "/". So Then I could store it as a variable. That variable would equal "muchprogram" to be clear.

I am also wondering how I could get everything before that final "/" as well. Thanks in advance.

Upvotes: 5

Views: 10895

Answers (5)

Ajinkya Kher
Ajinkya Kher

Reputation: 263

You can solve this in c# as follows..

Var tokens = Str.Split('/');
Var lastItem = tokens[tokens.Length-1];
Var everythingBeforeLastItem = string.Empty;
Enumerate.Range(0,tokens.Length-3).ToList().
ForEach(i => everythingBeforeLastItem = everythingBeforeLastItem+tokens[i]+"\");
EverythingBeforeLastItem += tokens[tokens.Length-2];

You can use StringBuilder for efficiency if you expect a deeper path resulting in large number of tokens..

Upvotes: -2

nodakai
nodakai

Reputation: 8001

strrchr(3) has been there since C89 for that purpose.

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

static void find_destructive(char *s) {
    char *p_sl = strrchr(s, '/');
    if (p_sl) {
        *p_sl = '\0';
        printf("[%s] [%s]\n", s, p_sl + 1);
    } else {
        printf("Cannot find any slashes.\n");
    }
}

static void find_transparent(const char *s) {
    const char *p_sl = strrchr(s, '/');
    if (p_sl) {
        char *first = (char *)malloc(p_sl - s + 1);
        if ( ! first) {
            perror("malloc for a temp buffer: ");
            return;
        }
        memcpy(first, s, p_sl - s);
        first[p_sl - s] = '\0';
        printf("[%s] [%s]\n", first, p_sl + 1);
        free(first);
    } else {
        printf("Cannot find any slashes.\n");
    }
}

int main() {
    char s[] = "home/usr/wow/muchprogram";

    find_transparent(s);
    find_destructive(s);

    return 0;
}

Upvotes: 0

Adam Burry
Adam Burry

Reputation: 1902

Someone else already suggested this, but they forgot to include the C. This assumes it is ok to mutate the source string. Tested with GCC 4.7.3.

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

int main() {
  char* s = "home/usr/wow/muchprogram";
  int n = strlen(s);
  char* suffix = s + n;

  printf("%s\n%s\n", s, suffix);

  while (0 < n && s[--n] != '/');
  if (s[n] == '/') {
    suffix = s + n + 1;
    s[n] = '\0';
  }

  printf("%s\n%s\n", s, suffix);
  return 0;
}

Upvotes: 1

HelloWorld123456789
HelloWorld123456789

Reputation: 5359

Start scanning the string from the end. Once you get a / stop. Note the index and copy from index+1 to last_index, to a new array.

You get everything before the final / as well. You have the index. Start copying from start_index to index-1, to a new array.

Upvotes: 1

Arkku
Arkku

Reputation: 42109

Search backwards from the end of the string until you find a '/'. The pointer to the next index from there is the string of everything after that. If you copy everything up to, but not including, the '/' into another string (or replace the '/' with '\0'), you obtain the string of everything before the last '/'.

Upvotes: 0

Related Questions