How to detect uppercase and lowercase characters in a string using pointers

Rishabh

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?

chux - Reinstate Monica

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.

edited at
0

Comments

0 comments
Login to comment

Related

Check If a substring is in a string ignoring uppercase, lowercase and special characters?

How to NOT convert uppercase to lowercase after using regex?

How to count uppercase and lowercase letters in a string?

How can I test if a letter in a string is uppercase or lowercase using JavaScript?

Convert uppercase string to lowercase using charAt

Swapping uppercase and lowercase in a string

How to uppercase/lowercase UTF-8 characters in C++?

how to make this lowercase and uppercase

How to change uppercase & lowercase alternatively in a string?

How do I alternatively uppercase and lowercase characters in a string?

How to return uppercase and lowercase string on different lines?

How to read uppercase as lowercase?

How to replace uppercase consonants with corresponding lowercase ones using c++ string?

Python Lowercase and Uppercase String

How do I differentiate between uppercase and lowercase characters in a case statement?

Detect if a string contains uppercase characters

How to see if a String Indifferent to uppercase or lowercase appears?

How to replace characters from uppercase to lowercase using bash, considering those characters are inside a <a href> tag?

How to get gsub! to recognize different characters (uppercase, lowercase)

Counting uppercase and lowercase characters of String and appending count to the character

How to print out a string in uppercase/lowercase letters strictly using the fgets function?

Converting Odd and Even-indexed characters in a string to uppercase/lowercase in Javascript?

Comparing string regardless of lowercase and uppercase characters in python

how to compare two string regardless of lowercase and uppercase using angular js

How to handle uppercase and lowercase characters when using UIKeyCommand

How to detect the group of characters in a string using RegEx?

JavaScript how to split conjoining string on UpperCase and LowerCase Characters

How to check if string contains both uppercase and lowercase characters

How to make Discord.py detect uppercase and lowercase word format?