1

I've been at this for quite some time now and the existing answers offer little to no help. I am new to programming and am trying to write a sub-part of my program which tries to check whether any given input is constituted solely of alphabets.

For this, the idea I have in mind is to pass an entire array through the isalpha function by using a loop which passes each character at a time. The idea makes logical sense but I am having syntactic trouble implementing it. I will greatly appreciate any help!

Below is my code-

printf("Please type the message which needs to be encrypted: ");
string p = GetString();

for (int i = 0, n = strlen(p); i < n; i++)
{
   if(isalpha(**<what I'm putting here is creating the problem, I think>**) = true)
   {
      printf("%c", p[i]);
   }

}
gsamaras
  • 71,951
  • 46
  • 188
  • 305

3 Answers3

6

You should modify your code as this (assuming you have the string type defined yourself):

printf("Please type the message which needs to be encrypted: ");
string p = GetString();

for (int i = 0, n = strlen(p); i < n; i++)
{
   if(isalpha(p[i]) == true) // HERE IS THE ERROR, YOU HAD =, NOT ==
   {
      printf("%c", p[i]);
   }

}

Operator = is for assignment and operator == is for comparison!

So what was happening? The assignment resulted in true, no matter what p[i] was.

As Quentin mentioned:

if(isalpha(p[i]) == true)

could be more elegant and error prune if written like this:

if(isalpha(p[i]))

Here is an example in C:

/* isalpha example */
#include <stdio.h>
#include <ctype.h>

int main(void)
{
  int i = 0;
  char str[] = "C++";
  while (str[i]) // strings in C are ended with a null terminator. When we meet
  // the null terminator, while's condition will get false.
  {
    if (isalpha(str[i])) // check every character of str
       printf ("character %c is alphabetic\n",str[i]);
    else
       printf ("character %c is not alphabetic\n",str[i]);
    i++;
  }
  return 0;
}

Source

Ref of isalpha().

C does not have a string type.

Tip: Next time post your code as it is!

Aslo, as Alter noticed, it would be nice to use:

isalpha((unsigned char)str[i])

and in your code

isalpha((unsigned char)p[i])

for safety reasons.

Community
  • 1
  • 1
gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • A cast `isalpha((unsigned char)str[i])` [is needed](http://stackoverflow.com/questions/17975913/does-ctype-h-still-require-unsigned-char) – David Ranieri Jun 03 '14 at 07:43
  • 1
    @G.Samaras- Thank you so much for explaining everything so well! I am teaching myself and often encounter roadblocks but its people like you who keep me going! Cheers :) – boametaphysica Jun 03 '14 at 07:58
  • Can you tell me how to do that? I can't upvote as I need 15 reputation. Additionally, I had another quick question- I tried to modify my code according to the suggestions but its throwing an "implicit declaration of function 'isalpha' error. Is there any way I can amend this? I also don't understand why is this error popping up :/ – boametaphysica Jun 03 '14 at 08:08
  • #include is the solution, as mentioned in my 2nd example. – gsamaras Jun 03 '14 at 08:10
2

Your example is here.

I.e. parameter of isalpha() is i-th character of string p. The only question is how to access to i-th character. Usually you can use []. I.e. just use following code: isalpha(p[i]) (I see that you already use [] in call of printf).

Also isalpha(p[i]) = true is wrong condition. It looks like you planned to check isalpha(p[i]) == true (you can skip == true).

Ilya
  • 4,583
  • 4
  • 26
  • 51
  • 1
    The problem of the OP is not there! – gsamaras Jun 03 '14 at 07:22
  • You are welcome. I believe you would notice that too, if the OP would have posted his/her code as is. Hopefully, (s)he will follow my tip in the future. I think the +2 you have are fair. – gsamaras Jun 03 '14 at 07:37
0

Late but:

both other answers say omitting == true is desirable, but don't say it is necessary for portability.

The C core-language operators == != < <= > >= && || which return a 'logical' value use an int value of 1 for true and 0 for false. In C99 and up with stdbool.h and by common convention before that true is 1 and false is 0, so e.g. if( (a < b) == true ) will work correctly, although it is redundant and many (including me) consider it poor style. Language elements that test a logical value, namely if(c) while(c) for(;c;) and the operands to && || and the left operand to ?: consider any value that compares equal to 0 to be false, and any other value to be true.

The character-classification routines in ctype.h as well as some other standard-library routines like feof(f) and ferror(f) are specified to return some nonzero int for true and 0 (an int) for false, and on many implementations the nonzero value used for true is not (always) 1. In those cases isalpha(whatever) == true might result in testing say 4 == 1 and fail even when whatever is an alphabetic character. OTOH isalpha(...) != false or isalpha(...) != 0 does work correctly if you really want to write something explicit.

dave_thompson_085
  • 34,712
  • 6
  • 50
  • 70