如何使用指针在字符串中检测大写和小写字符

huangapple go评论73阅读模式
英文:

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 &lt;stdio.h&gt;
#include &lt;string.h&gt;

int main()
{ 
	char s[20];
	int cu = 0, cl = 0, cs = 0;
	scanf(&quot;%s&quot;, s);
	printf(&quot;\n%s&quot;, s);
	for (int *i = s; *i != &#39;
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
int main()
{ 
char s[20];
int cu = 0, cl = 0, cs = 0;
scanf(&quot;%s&quot;, s);
printf(&quot;\n%s&quot;, s);
for (int *i = s; *i != &#39;\0&#39;; i++)
{
if ((*i &gt;= &#39;A&#39;) &amp;&amp; (*i &lt;= &#39;Z&#39;))
cu++;
else
if ((*i &gt;= &#39;a&#39;) &amp;&amp; (*i &lt;= &#39;z&#39;))
cl++;
else
cs++;
}
printf(&quot;\n uppercase:%d ,lowercase:%d ,others:%d&quot;, cu, cl, cs);
}
&#39;; i++) { if ((*i &gt;= &#39;A&#39;) &amp;&amp; (*i &lt;= &#39;Z&#39;)) cu++; else if ((*i &gt;= &#39;a&#39;) &amp;&amp; (*i &lt;= &#39;z&#39;)) cl++; else cs++; } printf(&quot;\n uppercase:%d ,lowercase:%d ,others:%d&quot;, 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 != &#39;
char s[20];
...
for (int *i = s; *i != &#39;\0&#39;; i++) {
warning: initialization of &#39;int *&#39; from incompatible pointer type &#39;char *&#39; [-Wincompatible-pointer-types]
&#39;; i++) { warning: initialization of &#39;int *&#39; from incompatible pointer type &#39;char *&#39; [-Wincompatible-pointer-types]

Save time, enable all warnings and use consistent pointer types.

// for (int *i = s; *i != &#39;
// for (int *i = s; *i != &#39;\0&#39;; i++) {
for (char *i = s; *i != &#39;\0&#39;; i++) {
&#39;; i++) { for (char *i = s; *i != &#39;
// for (int *i = s; *i != &#39;\0&#39;; i++) {
for (char *i = s; *i != &#39;\0&#39;; i++) {
&#39;; 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 &#39;int *&#39; with an expression of type &#39;char[20]&#39; [-Wincompatible-pointer-types]
    for(int *i=s;*i!=&#39;
testuplow.c:9:14: warning: incompatible pointer types initializing &#39;int *&#39; with an expression of type &#39;char[20]&#39; [-Wincompatible-pointer-types]
for(int *i=s;*i!=&#39;\0&#39;;i++)
^ ~
&#39;;i++) ^ ~

huangapple
  • 本文由 发表于 2023年6月22日 14:36:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76529148.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定