[英]How to implement a loop to read the array of names that checks for number of upper case letters etc?
程序告訴我有0個大寫字母0個小寫字母0個空格和0個制表符,但對於數組2個名稱均為小寫字母的數組,還有61個其他字符。 名稱由10個字母組成。 我想我需要一個循環來遍歷數組,但是我不確定這是否正確或我將如何執行。
for (i=0; i<n_names; i++){
printf("%d: [[%s]]\n", i, names[i]);}
for (i=0; i<20; i++){
gets(names[i]);
while (s[i] != 0)
{
if (s[i] >= 'a' && s[i] <= 'z') {
lowercase++;
i++;
}
else if (s[i] >= 'A' && s[i] <= 'Z') {
uppercase++;
i++;
}
else if (s[i] == ' ') { /* Tab - write '\t' for clarity! */
tab++;
i++;
}
else if (*names[i] == ' ') {
spaces++;
i++;
}
else {
other++;
i++;
}
}
}
printf("Your string has %d lowercase letter(s) \n",lowercase);
printf("Your string has %d uppercase letter(s) \n",uppercase);
printf("Your string has %d tab(s) \n",tab);
printf("Your string has %d space(s) \n", spaces);
printf("Your string has %d other character(s) \n",other);
我不認為您粘貼了整個代碼,我想您“ s”指向名稱中的字符串。 同時在while循環之前將“ i”初始化為零。
請使用標准函數islower,isupper,並且對於制表符,您需要與'\\ t'比較
ctype.h中針對您的方案的其他有用功能。
#include <ctype.h>
int isalnum(int c);
int isalpha(int c);
int isascii(int c);
int isblank(int c);
int iscntrl(int c);
int isdigit(int c);
int isgraph(int c);
int islower(int c);
int isprint(int c);
int ispunct(int c);
int isspace(int c);
int isupper(int c);
int isxdigit(int c);
int i, n_idx;
for (n_idx = 0; n_idx < n_names; n_idx++) {
const char *s = names[n_idx];
for (i = 0; s[i] != 0; i++) {
if (s[i] >= 'a' && s[i] <= 'z') {
lowercase++;
}
else if (s[i] >= 'A' && s[i] <= 'Z') {
uppercase++;
}
else if (s[i] == '\t') {
tab++;
}
else if (s[i] == ' ') {
spaces++;
}
else{
other++;
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.