For example, to validate the valid Url, I\'d like to do the following
char usUrl[MAX] = \"http://www.stackoverflow\"
if(usUrl[0] == \'h\'
&& usUr
strstr(str1, "http://www.stackoverflow")
is another function that can be used for this purpose.
A solution using an explicit loop:
#include <stdio.h>
#include <stddef.h>
#include <stdbool.h>
bool startsWith(const char *haystack, const char *needle) {
for (size_t i = 0; needle[i] != '\0'; i++) {
if (haystack[i] != needle[i]) {
return false;
}
}
return true;
}
int main() {
printf("%d\n", startsWith("foobar", "foo")); // 1, true
printf("%d\n", startsWith("foobar", "bar")); // 0, false
}
The following should check if usUrl starts with "http://":
strstr(usUrl, "http://") == usUrl ;
bool StartsWith(const char *a, const char *b)
{
if(strncmp(a, b, strlen(b)) == 0) return 1;
return 0;
}
...
if(StartsWith("http://stackoverflow.com", "http://")) {
// do something
}else {
// do something else
}
You also need #include<stdbool.h>
or just replace bool
with int
I would suggest this:
char *checker = NULL;
checker = strstr(usUrl, "http://");
if(checker == usUrl)
{
//you found the match
}
This would match only when string starts with 'http://'
and not something like 'XXXhttp://'
You can also use strcasestr
if that is available on you platform.