0

I'm stuck when I'm trying to identify whether the command-line argument input contains only alphabetical chars.

Here's my code:

enter image description here

When I tried to run the program, it showed an error message.

enter image description here

I don't know what went wrong in my code. Can anyone help?

3 Answers 3

2

The string argv[] refers to string of strings. So when you are trying to do argv[1] then it means that you are looking for a string, but you cannot use isalpha(string), because the argument passed to isalpha() is char. It should be like isalpha(argv[1][0]), where argv[1][0] refers to the first character in the second string in the list of strings.

1
  • isalpha() could be passed an integer as well.
    – user1892
    Jul 20, 2014 at 8:02
0

Well, one way to determine whether a string contains any non-alphabetical chars is to iterate over the chars of this string character by character, checking whether this character is an alphabetical character using a function like isalph().

The function isalpha() receives a char as an argument (not a whole string like argv[1]), returns true in case it's an alphabetical char and false otherwise.

The pseudocode for this might be something like

for (int i = 0 to i = the length of the string - 1, increment i by 1)
{
    if the current character in the string in an not an alphabetical char
    {
        print an appropriate error message
        return 1
    }
}

Given that a string is just an array of chars, you can access a char within a string using the bracket notation. For example

string s = "hello";
printf("%c\n", s[0]); // prints the 'h'
printf("%c\n", s[2]); // prints the 'l'
printf("%c\n", s[4]); // prints the 'o'

Given that argv[] is an array of strings, argv[x] accesses the x th string in that array and argv[x][y] accesses the y th char in the x th string in that array.

3
  • Then, can i use code like isdigit() to determine if the current character in the string is not an alphabetical character?
    – MDUM
    Jul 13, 2014 at 15:36
  • @MDUM well, for a character not being an alphabetical character, this doesn't necessarily mean that it's a digit! It can be a symbol for example!
    – kzidane
    Jul 13, 2014 at 15:38
  • @MDUM this of course doesn't mean that you can't use isdigit() anywhere in your program. Rather, it means that it's definitely better to use isalpha() in this specific situation!
    – kzidane
    Jul 13, 2014 at 15:56
0

If I understand what you're trying to do correctly, you want to check inside the else if() if the string has only alpha characters, but you can't exactly put a loop inside there. What you can do, however, is write a function that returns a certain value if after checking the entire string and putting that inside there. Something like

int isAllAlpha(char* s)
{
    int i = 0
    while (s[i])
    {
        if(s[i] is not alpha)
        {
            return 0
        }
        increment i
    }
    return 1
}

What this loop does is check if each digit is alpha. If it finds a non-alpha character before it reaches the end of the string, it immediately returns 0. If it reaches the end and did not find a non-alpha character, it returns 1. The while(s[i]), if you haven't seen something like that before, simply keeps going until it finds the NULL terminator of the string. You can then put that inside your else if() to have it check the entire string in one line.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .