I am new to C and recently started learning about pointers. I am trying to enter a string and then get the number of uppercase and lowercase characters in the string. I want to do it with the help of pointers.
This is my code:
#include <stdio.h>
#include <string.h>
int main()
{
char s[20];
int cu = 0, cl = 0, cs = 0;
scanf("%s", s);
printf("\n%s", s);
for (int *i = s; *i != '\0'; i++)
{
if ((*i >= 'A') && (*i <= 'Z'))
cu++;
else
if ((*i >= 'a') && (*i <= 'z'))
cl++;
else
cs++;
}
printf("\n uppercase:%d ,lowercase:%d ,others:%d", cu, cl, cs);
}
This is the output I get :
QWerTy12
QWerTy12
uppercase:0 ,lowercase:0 ,others:3
As you can see, it's incorrect. Can someone tell me what I am doing wrong?
Hmm, too much like @Barmar. Saving as wiki,
Key mistake: not compiling with all warnings enabled
Will all warnings enabled, the problem is quickly identified:
char s[20];
...
for (int *i = s; *i != '\0'; i++) {
warning: initialization of 'int *' from incompatible pointer type 'char *' [-Wincompatible-pointer-types]
Save time, enable all warnings and use consistent pointer types.
// for (int *i = s; *i != '\0'; i++) {
for (char *i = s; *i != '\0'; i++) {
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments