19

How do I find a substring from the string path "/user/desktop/abc/post/" using C/C++? I want to check if folder "abc" is present or not in that path.

Path is character pointer char *ptr = "/user/desktop/abc/post/";

Sam
  • 7,252
  • 16
  • 46
  • 65
CrazyCoder
  • 772
  • 5
  • 11
  • 31

6 Answers6

40

Use std::string and find.

std::string str = "/user/desktop/abc/post/";
bool exists = str.find("/abc/") != std::string::npos;
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
19

In C, use the strstr() standard library function:

const char *str = "/user/desktop/abc/post/";
const int exists = strstr(str, "/abc/") != NULL;

Take care to not accidentally find a too-short substring (this is what the starting and ending slashes are for).

unwind
  • 391,730
  • 64
  • 469
  • 606
11

Example using std::string find method:

#include <iostream>
#include <string>

int main (){
    std::string str ("There are two needles in this haystack with needles.");
    std::string str2 ("needle");

    size_t found = str.find(str2);
    if(found!=std::string::npos){ 
        std::cout << "first 'needle' found at: " << found << '\n';
    }

    return 0;
}

Result:

first 'needle' found at: 14.
radical7
  • 8,957
  • 3
  • 24
  • 33
DragonB
  • 119
  • 1
  • 2
0

Use strstr(const char *s , const char *t) and include<string.h>

You can write your own function which behaves same as strstr and you can modify according to your requirement also

char * str_str(const char *s, const char *t)
{
int i, j, k;
for (i = 0; s[i] != '\0'; i++) 
{
for (j=i, k=0; t[k]!='\0' && s[j]==t[k]; j++, k++);
if (k > 0 && t[k] == '\0')
return (&s[i]);
}
return NULL;
}
Omkant
  • 9,018
  • 8
  • 39
  • 59
  • This function looks more complicated than supposed to be... Why you are returning "nothing" at the end since function should return int parameter? – codewarrior Nov 02 '12 at 12:57
  • You can write your own, but doing so will in most cases be wrong. The functions in the c library are usually more efficient and better tested than the stuff you write yourself. – Klas Lindbäck Nov 02 '12 at 14:01
  • Yeah sure it can fail in some cases but ... i tried and i did that so thought of pasting it here... – Omkant Nov 02 '12 at 14:59
0

As user1511510 has identified, there's an unusual case when abc is at the end of the file name. We need to look for either /abc/ or /abc followed by a string-terminator '\0'. A naive way to do this would be to check if either /abc/ or /abc\0 are substrings:

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

int main() {
    const char *str = "/user/desktop/abc";
    const int exists = strstr(str, "/abc/") || strstr(str, "/abc\0");
    printf("%d\n",exists);
    return 0;
}

but exists will be 1 even if abc is not followed by a null-terminator. This is because the string literal "/abc\0" is equivalent to "/abc". A better approach is to test if /abc is a substring, and then see if the character after this substring (indexed using the pointer returned by strstr()) is either a / or a '\0':

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

int main() {
    const char *str = "/user/desktop/abc", *substr;
    const int exists = (substr = strstr(str, "/abc")) && (substr[4] == '\0' || substr[4] == '/');
    printf("%d\n",exists);
    return 0;
}

This should work in all cases.

1''
  • 26,823
  • 32
  • 143
  • 200
  • 2
    *"`exists = (substr = strstr(str, "/abc")) && (substr[4] == '\0' || substr[4] == '/');` should work in all cases"* - not so... given say `"/abcd/abc"` you'll choke on the `d`... to do it properly, if the `substr[4]` test fails you need to resume the search further into the string. (It's probably easier to add a trailing '/' before searching for `/abc/`.) – Tony Delroy Jun 13 '15 at 13:49
  • @TonyD Good catch! I agree completely, feel free to edit the post. – 1'' Jun 14 '15 at 03:37
-1

If you are utilizing arrays too much then you should include cstring.h because it has too many functions including finding substrings.

shim
  • 9,289
  • 12
  • 69
  • 108
NatiSpark
  • 1
  • 1