如何使用if
, string[i]
(i
是一系列帽子字符串的数字) 比较字符串的每个字符。该程序的想法是为a = 1,b = 2之类的字母赋值,如果我在控制台中写一个单词(“a,b,a,b”),我会得到1 + 2 +的总和1+2。我想比较字符串的每个字符,直到字符串的结尾。像这样的东西:
#include <stdio.h >
int main ()
{
char string1[20];
char a='a', b='b', g='g', X='X', i='i', H='H', V='V', d='d', e='e', v='v', z='z', E='E', T='T', I='I', k='k', l='l', m='m', n='n', o='o', p='p', j='j', r='r', s='s', t='t', u='u', f='f', q='q', R='R', y='y', S='S', w='w', C='C', W='W', x='x', h='h', c='c', J='J', Z='Z';
int rigi;
int sum;
printf (" my word is:\n");
scanf( "%19s ", string1);
sum=0;
for (rigi = 0; string1[rigi] !='\0'; rigi++)
{
if (string1[rigi]==a ) {sum= sum +1;} else
if (string1[rigi]==b ) {sum= sum +2;} else
if (string1[rigi]==g ) {sum= sum +3;} else
if (string1[rigi]==d ) {sum= sum +4;} else
if (string1[rigi]==e ) {sum= sum +5;} else
if (string1[rigi]==v ) {sum= sum +6;} else
if (string1[rigi]==z ) {sum= sum +7;} else
if (string1[rigi]==E ) {sum= sum +8;} else
if (string1[rigi]==T ) {sum= sum +9;} else
if (string1[rigi]==i ) {sum= sum +10;} else
if (string1[rigi]==k ) {sum= sum +20;} else
if (string1[rigi]==l ) {sum= sum +30;} else
if (string1[rigi]==m ) {sum= sum +40;} else
if (string1[rigi]==n ) {sum= sum +50;} else
if (string1[rigi]==I ) {sum= sum +60;} else
if (string1[rigi]==o ) {sum= sum +70;} else
if (string1[rigi]==p ) {sum= sum +80;} else
if (string1[rigi]==J ) {sum= sum +90;} else
if (string1[rigi]==r ) {sum= sum +100;} else
if (string1[rigi]==s ) {sum= sum +200;} else
if (string1[rigi]==t ) {sum= sum +300;} else
if (string1[rigi]==V ) {sum= sum +400;} else
if (string1[rigi]==u ) {sum= sum +0;} else
if (string1[rigi]==f ) {sum= sum +500;} else
if (string1[rigi]==q ) {sum= sum +600;} else
if (string1[rigi]==R ) {sum= sum +700;} else
if (string1[rigi]==y ) {sum= sum +800;} else
if (string1[rigi]==S ) {sum= sum +900;} else
if (string1[rigi]==C ) {sum= sum +1000;} else
if (string1[rigi]==c ) {sum= sum +2000;} else
if (string1[rigi]==Z ) {sum= sum +3000;} else
if (string1[rigi]==w ) {sum= sum +4000;} else
if (string1[rigi]==W ) {sum= sum +5000;} else
if (string1[rigi]==x ) {sum= sum +6000;} else
if (string1[rigi]==X ) {sum= sum +7000;} else
if (string1[rigi]==j ) {sum= sum +8000;} else
if (string1[rigi]==h ) {sum= sum +9000;} else
if (string1[rigi]==H ) {sum= sum +1000;} else
{sum=0;}
}
printf ("%d\n" , sum);
return 0;
}