英文:
How to detect uppercase and lowercase characters in a string using pointers
问题
这是我的代码:
#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 (char *i = s; *i != '#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 (char *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);
}
'; 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);
}
这是我得到的输出:
QWerTy12
QWerTy12
uppercase:3 ,lowercase:6 ,others:2
正如你所看到的,这次输出是正确的。你之前的问题在于,你的指针 i
应该是指向字符(char
)而不是整数(int
),而且你的条件语句也需要使用单引号来表示字符,如 'A'
和 'Z'
。我已经修复了这些问题。
英文:
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 != '#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);
}
'; 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?
答案1
得分: 1
太像 @Barmar。保存为维基页面,
关键错误:未启用所有警告进行编译
启用所有警告后,问题迅速被识别:
char s[20];
...
for (int *i = s; *i != 'char s[20];
...
for (int *i = s; *i != '\0'; i++) {
warning: initialization of 'int *' from incompatible pointer type 'char *' [-Wincompatible-pointer-types]
'; i++) {
warning: initialization of 'int *' from incompatible pointer type 'char *' [-Wincompatible-pointer-types]
节省时间,启用所有警告并使用一致的指针类型。
// for (int *i = s; *i != '// for (int *i = s; *i != '\0'; i++) {
for (char *i = s; *i != '\0'; i++) {
'; i++) {
for (char *i = s; *i != '// for (int *i = s; *i != '\0'; i++) {
for (char *i = s; *i != '\0'; i++) {
'; i++) {
英文:
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 != 'char s[20];
...
for (int *i = s; *i != '\0'; i++) {
warning: initialization of 'int *' from incompatible pointer type 'char *' [-Wincompatible-pointer-types]
'; 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 != '// for (int *i = s; *i != '\0'; i++) {
for (char *i = s; *i != '\0'; i++) {
'; i++) {
for (char *i = s; *i != '// for (int *i = s; *i != '\0'; i++) {
for (char *i = s; *i != '\0'; i++) {
'; i++) {
答案2
得分: 0
你不能使用 int *
指针来访问字符串的字符。当你解引用指针时,它会读取多个字符(如果 int
是 32 位,则读取 4 个字符),而 i++
会增加多个字符。
你必须使用 char *
,所以将 int *i
改为 char *i
。
请确保在编译器选项中启用完整的警告,并注意这些警告。当我尝试编译你的代码时,我得到了以下警告:
testuplow.c:9:14: warning: incompatible pointer types initializing 'int *' with an expression of type 'char[20]' [-Wincompatible-pointer-types]
for(int *i=s;*i!='testuplow.c:9:14: warning: incompatible pointer types initializing 'int *' with an expression of type 'char[20]' [-Wincompatible-pointer-types]
for(int *i=s;*i!='\0';i++)
^ ~
';i++)
^ ~
英文:
You can't use an int *
pointer to access characters of a string. When you dereference the pointer it will read multiple characters (4 characters if int
is 32 bits) and i++
will increment by multiple characters.
You have to use char *
, so change int *i
to char *i
.
Make sure you enable full warnings in your compiler options, and heed the warnings. When I tried to compile your code I got this warning:
testuplow.c:9:14: warning: incompatible pointer types initializing 'int *' with an expression of type 'char[20]' [-Wincompatible-pointer-types]
for(int *i=s;*i!='testuplow.c:9:14: warning: incompatible pointer types initializing 'int *' with an expression of type 'char[20]' [-Wincompatible-pointer-types]
for(int *i=s;*i!='\0';i++)
^ ~
';i++)
^ ~
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论