20

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(). Commented Nov 2, 2012 at 12:35
  • This is 2 questions in 1, C and C++ are two totally different languages. Commented Jun 26, 2020 at 4:27

6 Answers 6

43

Use std::string and find.

std::string str = "/user/desktop/abc/post/";
bool exists = str.find("/abc/") != std::string::npos;
Sign up to request clarification or add additional context in comments.

5 Comments

what happen if "/user/desktop/abc" then how findout "abc" folder
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."?
@user1511510 no, npos is returned by find if the substring wasn't found.
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?
@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.
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 Comments

hmm.. what if it's the last part of the path?
what happen if "/user/desktop/abc" then how findout "abc" folder.
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.

Comments

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 Comments

This function looks more complicated than supposed to be... Why you are returning "nothing" at the end since function should return int parameter?
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.
Yeah sure it can fail in some cases but ... i tried and i did that so thought of pasting it here...
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 Comments

"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/.)
@TonyD Good catch! I agree completely, feel free to edit the post.
-1

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

1 Comment

You mean, we dont have any standard function to find out the substring from the string.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.