简体   繁体   中英

A check in C language if there is a space inside the array string?

How can I make a check if there is a space inside the array string? Could you help me? Thanks in advance ;)

for ( i = 0;  string[i] != '\0';  ++i )
        if ( string[i]) != ' ' )

error: expected expression

You can use standard function strchr(string, ' ') - return a non-NULL pointer if string has space inside:

if(strchr(string, ' '))
{
    // printf( "string contain space" );
}

Refer to strchr function:

7.24.5.2 The strchr function

     #include <string.h>
     char *strchr(const char *s, int c);

3 The strchr function returns a pointer to the located character, or a null pointer if the character does not occur in the string.

You provided an additional ) in your if condition

if ( string[i]) != ' ' )
//here--------^

remove it or balance it with (

if (string[i] != ' ' )
//OR
if ((string[i]) != ' ' )

Always make sure to check for equal number of ( and ) in a program ;)

Since you wanted to check whether space is present in an array or not, you can do something like this:

for ( i = 0;  string[i] != '\0';  ++i )
    if (string[i] != ' ' )
       break;

if(string[i] != '\0')
    printf("space found at index no: %d", i); 

If you want to know the total number of spaces present in the array, use a variable as counter and increment it whenever a ' ' is found:

int count = 0;

for ( i = 0;  string[i] != '\0';  ++i )
    if (string[i] != ' ' )
       count++

Like I said in my Comment there is a problem with the Title of your Question and with your provided code. Both are different things.

Your Code is OK if you remove an extra ) located here:

if ( string[i]) != ' ' )

Try this Code:

#include <stdio.h>

int main(void){
    const char *string = "This is a String";
    int i = 0;
    int count = 0;

    for ( i = 0;  string[i] != '\0';  ++i ){
        if ( string[i] != ' ' ){
            count++;
        }
    }

    printf("Number of Letters found are: %d\nNumber Of spaces Found are %d\n", count, (i - count));
}

Output:

Number of Letters found are: 13
Number Of spaces Found are 3

It depends what you mean by "space".

The standard header <ctype.h> specifies a function isspace() which tests if its argument corresponds to whitespace in the current locale. There are multiple characters for which isspace() can return true (eg in the C locale, which most english speakers will use, it returns true for the space character, newline, horizontal and vertical tabs, carriage return, and form feed).

If you want only a single character to be deemed a space, use strchr() . If you want to specify a set of characters that are all considered whitespace, use strpbrk() . Both of these functions care declared in <string.h> .

If you don't want to use those functions, then for whatever test you decide on for determining if a single character is a space, simply loop over all elements of the string, and test each one. With "standard strings" (like string literals, such as "Hello there" this means iterating until a character with value zero ( '\\0' ) is found.

BTW: the reason for the compiler error you mention is an extra ) on the second line.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM