Since C-style strings are always terminated with the null character (\0), you can check whether the string is empty by writing
do {
...
} while (url[0] != '\0');
Alternatively, you could use the strcmp function, which is overkill but might be easier to read:
do {
...
} while (strcmp(url, ""));
Note that strcmp returns a nonzero value if the strings are different and 0 if they're the same, so this loop continues to loop until the string is empty.
Hope this helps!
Answer from templatetypedef on Stack OverflowSince C-style strings are always terminated with the null character (\0), you can check whether the string is empty by writing
do {
...
} while (url[0] != '\0');
Alternatively, you could use the strcmp function, which is overkill but might be easier to read:
do {
...
} while (strcmp(url, ""));
Note that strcmp returns a nonzero value if the strings are different and 0 if they're the same, so this loop continues to loop until the string is empty.
Hope this helps!
If you want to check if a string is empty:
if (str[0] == '\0')
{
// your code here
}
Videos
I need to simply check if an object in a string vector is null or not:
std::vector<string> vliwSlots;
//Some inserts...
string vliwSlot = vliwSlots.at(i); //in a for loop
if(vliwSlot != NULL){ //error on this line
//Do stuff
}When I compile this I get the following error at :
no match for โoperator!=โ (operand types are โstd::__cxx11::string {aka std::__cxx11::basic_string<char>}โ and โlong intโ)
It seems it thinks NULL is a long int. How the heck do I check if a string is null?
The easy way to do it would be like this:
if (msg[0])
printf("message: %s", msg);
If, at some later date, msg is a pointer, you would first want to assure it's not a NULL pointer.
if (msg && msg[0])
printf("message: %s", msg);
A test program to demonstrate:
#include <stdio.h>
#include <stdlib.h>
char *msg;
void test() {
if (msg && msg[0])
printf("string is %s\n", msg);
}
int main()
{
msg = NULL;
test();
msg = calloc(10, 1);
test();
msg[0] = 'm';
msg[1] = 'e';
test();
free(msg);
}
On my machine the output is:
string is me
Strings in C are represented as character arrays, terminated by a character whose value is zero (often written as '\0'). The length of a string is the number of characters that come before the zero.
So, a string is empty if the first character in the array is zero, since then by definition no characters came before the zero.
This can be checked like so:
if(msg[0] != '\0')
{
/* string isn't empty! */
}
This is very explicit code, the shorter if(msg[0]) or if(*msg) has the exact same semantics but can be slightly harder to read. It's mostly a matter of personal style.
Note though that in your case, when you use scanf() to read into msg, you should check the return value from scanf() before checking the contents of msg. It's perfectly possible for scanf() to fail, and then you can't rely on msg being set to an empty string.
Check if the first character is '\0'. You should also probably check if your pointer is NULL.
Copychar *c = "";
if ((c != NULL) && (c[0] == '\0')) {
printf("c is empty\n");
}
You could put both of those checks in a function to make it convenient and easy to reuse.
Edit: In the if statement can be read like this, "If c is not zero and the first character of character array 'c' is not '\0' or zero, then...".
The && simply combines the two conditions. It is basically like saying this:
Copyif (c != NULL) { /* AND (or &&) */
if (c[0] == '\0') {
printf("c is empty\n");
}
}
You may want to get a good C programming book if that is not clear to you. I could recommend a book called "The C Programming Language".
The shortest version equivalent to the above would be:
Copyif (c && !c[0]) {
printf("c is empty\n");
}
My preferred method:
Copyif (*ptr == 0) // empty string
Probably more common:
Copyif (strlen(ptr) == 0) // empty string
When I try using .isEmpty() or isspace() it gives me the message that "expression must have class type" and str != "" doesn't seem to work. The type I'm using is "const char *".
Given this code:
char text[50];
if(strlen(text) == 0) {}
Followed by a question about this code:
memset(text, 0, sizeof(text));
if(strlen(text) == 0) {}
I smell confusion. Specifically, in this case:
char text[50];
if(strlen(text) == 0) {}
... the contents of text[] will be uninitialized and undefined. Thus, strlen(text) will return an undefined result.
The easiest/fastest way to ensure that a C string is initialized to the empty string is to simply set the first byte to 0.
char text[50];
text[0] = 0;
From then, both strlen(text) and the very-fast-but-not-as-straightforward (text[0] == 0) tests will both detect the empty string.
Depends on whether or not your array is holding a null-terminated string. If so, then
if(text[0] == '\0') {}
should be sufficient.
Edit: Another method would be...
if (strcmp(text, "") == 0)
which is potentially less efficient but clearly expresses your intent.