Reputation: 11
int isEmpty(char x [1000]){
int i = 0;
while( x[i] == " " || x[i] == "/t" || x[i] == ""){
i++;
}
if (i != 999)
return 1;
}
return 0;
}
The errors I recieve:
warning: comparison between pointer and integer
warning: comparison with string literal results in unspecified behavior
I realise my code reads far too much like Java, I haven't fully grapled with the C syntax yet.
Upvotes: 1
Views: 394
Reputation: 3990
int isEmpty(const char *x)
{
return EOF==sscanf(x,"%*s");
}
should also do what you want, is C89 and test whitespaces for you.
Upvotes: 0
Reputation: 20403
In addition to the previous answers, I think
NULL (\0)
is found in x
? I think that answer might be: NO.(i != 999)
part is unclear. Won't it be (i == 1000)
?Here is my version:
/* return 1 when c is considered empty, 0 otherwise */
int isEmptyChar( const char c ) {
return c == ' ' || c == '\t';
}
enum { N = 1000 };
/* return 1 when x is considered empty, 0 otherwise */
int isEmptyString( const char x[ N ] ) {
for( int i = 0; i < N; i++ ) {
if( x[ i ] == '\0' ) {
break; /* stop, if NUL termination is found */
}
if( ! isEmptyChar( x[ i ] ) ) {
return 0; /* early return if non-empty char is found */
}
}
return 1;
}
Upvotes: 0
Reputation: 455400
Since you are comparing with char constants, you need to use single quotes and not double quotes as:
x[i] == ' '
Double quotes are used for string constants like "foo"
. So "\n"
is a string literal with a single char and '\n'
is a char constant.
Upvotes: 2
Reputation: 755457
The problem is you're expressing char
values by using string literals. Instead of surrounding them with double quotes "
use single quotes '
while( x[i] == ' ' || x[i] == '\t' ) {
Couple of other issues.
\t
and not /t
. The former is a tab character while the latter is two characters""
. Perhaps \0
Upvotes: 5
Reputation:
' '
(single quotes).\t
& not /t
for the tab character.""
(it's equivalent to a NULL
).Upvotes: 0