Home > Back-end >  Can someone explain me why I am not able to check special characters in my string using ASCII values
Can someone explain me why I am not able to check special characters in my string using ASCII values

Time:09-02

So, my code actually works perfectly when I use function that checks for special characters in string by explicitly giving symbols:

int isSpecialCharacter(char str[], int n)
{
    for(int i=0; i<n;i  )
    {
        if (str[i] == '!' || str[i] == '@' || str[i] == '#' || str[i] == '$' || str[i] == '%' || str[i] == '^' || str[i] == '(' || str[i] == ')')
        {
            return 1;
        } 
        return 0;
    }
}

However, in the below code I am not able to get my code to find special characters in string using ASCII values. I want to understand what am I doing wrong. In the below code, I have my main() also included that prompts the user to input string.

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

char str[30][30];
char test[100];
int myindex = 0;

int isSpecialCharacter(char str[], int n)
{
    for(int i=0; i<n;i  )
    {
        if (str[i] == 33 || str[i] == 35 || str[i] == 36 || str[i] == 37 || str[i] == 40 || str[i] == 41 || str[i] == 64)
        {
            return 1;
        } 
        return 0;
    }
}

int main()
{
    printf("Please enter 10 strings below: \n");
    while(myindex < 10)
    {
        printf("Enter string %d: ", myindex 1);
        fgets(str[myindex], 500, stdin);
        strcpy(test, str[myindex]);
        int t_size = strlen(test);

        if (strlen(str[myindex])<2||strlen(str[myindex])>26)
        {
            printf("Enter string that is between 2 and 25");
            continue;

        }
        else if(isSpecialCharacter(test, t_size) == 1)
        {
            printf("Your string has some special characters. \n");
            continue;
        }
        else
        {
            myindex  ;
        }
    }
    return 0;
}

EDIT: I have included the variable declarations and replaced the code where I use symbols to check for special characters. Thank you.

EDIT2: These are the characters that I want to look for: : ’!’, ’@’, ’#’, ’$’, ‘%’, ‘^’, ’(’, or ’)’. Not spaces.

CodePudding user response:

The main problem with your function is that your for loop returns after each loop cycle, so you never check beyond the first cycle.
You should move the return 0 statement to the end of the function, then it will work as expected.

int isSpecialCharacter(char str[], int n)
{
    for (int i = 0; i < n; i  )
    {
        if (str[i] == 33 || str[i] == 35 || str[i] == 36 || str[i] == 37 || str[i] == 40 || str[i] == 41 || str[i] == 64)
        {
            return 1;
        }
    }
    return 0;
}

Test

I've tested the solution with the following input:

test1
test2
test)
test(
test3
test4
test5
test@
test6
test7
test!
test8
test9
test10

And it works. You can see the result there: https://godbolt.org/z/67Px14z75

CodePudding user response:

Your return 0; is incorrect:

int isSpecialCharacter(char str[], int n) {
    for(int i=0; i<n;i  )
    {
        if (str[i] == '!' || str[i] == '@' || str[i] == '#' || str[i] == '$' || str[i] == '%' || str[i] == '^' || str[i] == '(' || str[i] == ')')
        {
            return 1;
        } 
        return 0;
    } 
} 

On the very first loop, your function returns either 1 or 0. You only want to return 0 (false) if the loop completes without finding the character you're looking for.

int isSpecialCharacter(char str[], int n)
{
    for(int i=0; i<n;i  )
    {
        if (str[i] == '!' || str[i] == '@' || str[i] == '#' || str[i] == '$' || str[i] == '%' || str[i] == '^' || str[i] == '(' || str[i] == ')')
        {
            return 1;
        } 
    }
 
    return 0;
}

CodePudding user response:

I'm not going to try to write your code...

if( isSpecialCharacter()

is equivalent to:

if( strpbrk( test, "abqmz" ) != NULL )

(except that the latter has been proven to work...)

If a, b, q, m, z appear in the string, then the if condition is true...

Learn the standard library instead of spending time badly reinventing it...

EDIT:

Here is how an experienced programmer might write that functionality now that you've decoded the decimal ASCII values. (Don't expect the next programmer who reads your code to reach for their ASCII table...)

char spcl[] = "!@#$%^()";

if( strpbrk( test, spcl ) != NULL ) {
    // Do something because condition is true

NB: if you need to make either \ or " special inside a string, you must 'escape' either of those characters with an 'extra' backslash. Eg:

char spcl[] = "\\!@#$%^(\")"; // notice '\\' and '\""

If that looks too much like "bad language", you can construct your own string (ENSURING(!) that it is null terminated.)

char spcl[] = { '\\', '!', '@', '#', '$', '%', '^', '(', '\"', ')' '\0' };

And...

char str[30][30];

/* Skipping ahead */

printf("Enter string %d: ", myindex 1);
fgets(str[myindex], 500, stdin); // 500!!! ????

is simply looking for trouble...

  •  Tags:  
  • c
  • Related