簡體   English   中英

如何計算二維字符串中每個單詞的大寫字母

[英]How to count uppercase letter in each word in a 2D string

我試圖使用 C 找出 2D 字符串中每個單詞的大寫字母。 但我沒有弄清楚邏輯。 我假設這個結果 -

樣本輸入

2
Dhaka
apple

樣品 Output

1
0

這是我的代碼 -

#include<stdio.h>
#include<string.h>

int main()
{
    int count = 0, num;
    char str[50][50];

    printf("How many word you want to enter: ");
    scanf("%d", &num);

    printf("\nEnter any word: ");

    for(int i=0; i<num; i++)
    {
        for(int j=0; j<num; j++)
        {
            gets(str);
        }
    }

    for(int i=0; str[i]!='\0'; i++)
    {
        for(int j=i+1; j<num; j++)
        {
            if(str[i] >= 'A' && str[i] <= 'Z')
            {
                count++;
            }
        }
    }

    printf("\n%d\n", count);

    return 0;
}

資源:

#include<stdio.h>
#include<string.h>
#include<ctype.h>

int main(){
    int count = 0, num;
    //Enter how many lines you want to enter
    printf("How many lines you want to enter: ");
    scanf("%d", &num);

    char str[num][100];

    //printf("\nEnter any word: ");

    for(int i=0; i<num; i++){
        //flushs the previous buffer
        getchar();
        //Press enter to enter a line
        scanf("%[^\n]",str[i]);
    }

    for(int i=0;i<num; i++,count=0){
        for(int j=0; str[i][j]!='\0'; j++){
            //printf("%c",str[i][j]);

            //if(str[i][j] >= 'A' && str[i][j] <= 'Z'){
            //  count++;
            //}
            if( isupper(str[i][j]) )
                count++;
        }
        printf("\n%d\n",count);
    }

    return 0;
}

Output:

$ gcc string.c && ./a.out
How many lines you want to enter: 3
djjdjd JJ
j3j3j3j JJJJJKK
$#jjd

2

7

0

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM