cascii

Why am I unable to check special characters in my string using ASCII values?


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.


Solution

  • Both your functions won't work as you've written them.

    The main problem is that the 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


    As suggested from other users in the comment section, using decimal values to refer to characters is a bad practice™ and you should stick with the '@' notation.