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/";

2
  • 3
    strstr() or std::string::find().
    – hmjd
    Nov 2, 2012 at 12:35
  • This is 2 questions in 1, C and C++ are two totally different languages.
    – hyde
    Jun 26, 2020 at 4:27

6 Answers 6

40

Use std::string and find.

std::string str = "/user/desktop/abc/post/";
bool exists = str.find("/abc/") != std::string::npos;
5
  • what happen if "/user/desktop/abc" then how findout "abc" folder
    – CrazyCoder
    Nov 2, 2012 at 14:01
  • std::string::npos, its compare with initial position ? I am not getting what you told to j"ust check if it's the last position."?
    – CrazyCoder
    Nov 2, 2012 at 14:24
  • @user1511510 no, npos is returned by find if the substring wasn't found. Nov 2, 2012 at 14:24
  • I am not getting what you told to "just check if it's the last position." in the case "user/desktop/abc", how to findout "abc" folder?
    – CrazyCoder
    Nov 2, 2012 at 14:29
  • @user1511510 oh... well, I'm not going to write the code for you. Can't you figure it out yourself? It's just treating a corner case, I hope you can manage. Nov 2, 2012 at 14:32
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).

2
  • 7
    hmm.. what if it's the last part of the path? Nov 2, 2012 at 13:04
  • 1
    what happen if "/user/desktop/abc" then how findout "abc" folder.
    – CrazyCoder
    Nov 2, 2012 at 14:00
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.
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;
}
3
  • This function looks more complicated than supposed to be... Why you are returning "nothing" at the end since function should return int parameter? Nov 2, 2012 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. Nov 2, 2012 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 2, 2012 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.

2
  • 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/.) Jun 13, 2015 at 13:49
  • @TonyD Good catch! I agree completely, feel free to edit the post.
    – 1''
    Jun 14, 2015 at 3:37
-1

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

1
  • You mean, we dont have any standard function to find out the substring from the string.
    – CrazyCoder
    Nov 2, 2012 at 14:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.