Untitled

 avatar
unknown
plain_text
3 years ago
1.1 kB
2
Indexable
#include <stdio.h>
#include <string.h>
#include <ctype.h>

int main(void)
{
    int i,j,x;
    char worth[26];
    scanf("%s",worth); //輸入C1~C26
    setbuf(stdin, NULL); //清空清空buffer
    char input[101]={'\0'};
    while (gets(input)) 
    {
        if(input[0]=='e' && input[1]=='n' && input[2]=='d' && input[3]=='\0') //若只有輸入end則跳出迴圈結束
        {
            break;
        }
        else
        {
            x=0;
            for(i=0;i<strlen(input);i++)
            {
                for(j=0;j<26;j++)
                {
                    if(input[i]==worth[j])//比較input字母對應到哪一個worth裡的字母,若有則+J+1,且跳出迴圈找下一個字母
                    {
                        x=x+j+1;
                        break;
                    }
                }
                
            }
            if(x!=0) //若有找到則x!=0
            {
                printf("%d\n",x);
            }
            else //若沒找到則x=0,跳出迴圈
            {
                break;
            }
        }
    }
    
}
Editor is loading...