How to Check String Start in C++

How to check if a string starts with another string in C?

Apparently there's no standard C function for this. So:

bool startsWith(const char *pre, const char *str)
{
size_t lenpre = strlen(pre),
lenstr = strlen(str);
return lenstr < lenpre ? false : memcmp(pre, str, lenpre) == 0;
}

Note that the above is nice and clear, but if you're doing it in a tight loop or working with very large strings, it does not offer the best performance, as it scans the full length of both strings up front (strlen). Solutions like wj32's or Christoph's may offer better performance (although this comment about vectorization is beyond my ken of C). Also note Fred Foo's solution which avoids strlen on str (he's right, it's unnecessary if you use strncmp instead of memcmp). Only matters for (very) large strings or repeated use in tight loops, but when it matters, it matters.

Checking the first letter of a string in c

Don't have access to a compiler, but I think this will work because C-style strings are just arrays with a termination character:

int isAbsolute(const char *str){
return (str[0] == '/');
}

Check substring exists in a string in C

if (strstr(sent, word) != NULL) {
/* ... */
}

Note that strstr returns a pointer to the start of the word in sent if the word word is found.

how to check string start in C++

std::string s("Hello world");

if (s.find("Hello") == 0)
{
std::cout << "String starts with Hello\n";
}

Check if a string starts with another known string?

http://ideone.com/w1ifiJ

#include <iostream>
using namespace std;

int main() {
string str ("abcdefghijklmnoabcde");
string str2 ("abcde");

size_t found = str.find(str2);

if(found == 0)
{
cout << "found";
}

return 0;
}

more info : http://www.cplusplus.com/reference/string/string/find/

How do I check if a C++ std::string starts with a certain string, and convert a substring to an int?

Use rfind overload that takes the search position pos parameter, and pass zero for it:

std::string s = "tititoto";
if (s.rfind("titi", 0) == 0) { // pos=0 limits the search to the prefix
// s starts with prefix
}

Who needs anything else? Pure STL!

Many have misread this to mean "search backwards through the whole string looking for the prefix". That would give the wrong result (e.g. string("tititito").rfind("titi") returns 2 so when compared against == 0 would return false) and it would be inefficient (looking through the whole string instead of just the start). But it does not do that because it passes the pos parameter as 0, which limits the search to only match at that position or earlier. For example:

std::string test = "0123123";
size_t match1 = test.rfind("123"); // returns 4 (rightmost match)
size_t match2 = test.rfind("123", 2); // returns 1 (skipped over later match)
size_t match3 = test.rfind("123", 0); // returns std::string::npos (i.e. not found)

C++ How to check if string starts with char from input

You need to access first element of the string:

std::string someString = "foo"; // for example purposes
char someChar = 'f';

if (someString[0] == someChar)
std::cout << "First character of this string is " << someChar << "!";

Be careful though, if you fail to read string, you can try to access the first element (with index 0) but it may not exist. Do it like this:

if (someString.length() > 0)
{
// string is not empty. You can use also !someString.empty()
}

How do I check if a string contains a certain character?

By using strchr(), like this for example:

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

int main(void)
{
char str[] = "Hi, I'm odd!";
int exclamationCheck = 0;
if(strchr(str, '!') != NULL)
{
exclamationCheck = 1;
}
printf("exclamationCheck = %d\n", exclamationCheck);
return 0;
}

Output:

exclamationCheck = 1

If you are looking for a laconic one liner, then you could follow @melpomene's approach:

int exclamationCheck = strchr(str, '!') != NULL;

If you are not allowed to use methods from the C String Library, then, as @SomeProgrammerDude suggested, you could simply iterate over the string, and if any character is the exclamation mark, as demonstrated in this example:

#include <stdio.h>

int main(void)
{
char str[] = "Hi, I'm odd";
int exclamationCheck = 0;
for(int i = 0; str[i] != '\0'; ++i)
{
if(str[i] == '!')
{
exclamationCheck = 1;
break;
}
}
printf("exclamationCheck = %d\n", exclamationCheck);
return 0;
}

Output:

exclamationCheck = 0

Notice that you could break the loop when at least one exclamation mark is found, so that you don't need to iterate over the whole string.


PS: What should main() return in C and C++? int, not void.



Related Topics



Leave a reply



Submit