Click here to Skip to main content
15,878,852 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
#include<stdio.h>
#include<string.h>

int main()
{
    int c,cas,i,j,sum=0;
    char a[100];
    scanf("%d", &cas);
    for(i=1;i<=cas;i++)
        {
            gets(a);
            c = strlen(a);
            for(j=1;j<=c;j++)
            {
               if(a[j]=='a' || a[j]=='d' || a[j]=='g' || a[j]=='j' || a[j]=='m' || a[j]=='p' || a[j]=='t' || a[j]=='w' || a[j]==' ')
                sum += 1;
               else if(a[j]=='b' || a[j]=='e' || a[j]=='h' || a[j]=='k' || a[j]=='n' || a[j]=='q' || a[j]=='u' || a[j]=='x')
                sum += 2;
               else if(a[j]=='c' || a[j]=='f' || a[j]=='i' || a[j]=='l' || a[j]=='o' || a[j]=='r' || a[j]=='v' || a[j]=='y')
                sum += 3;
               else if(a[j]=='s' || a[j]=='z' )
                sum += 4;
            }
            printf("Case #%d: %d\n",i,sum);
        }
return 0;
}
Posted
Updated 17-Jan-15 16:42pm
v2
Comments
Sergey Alexandrovich Kryukov 17-Jan-15 23:38pm    
Tried to use a debugger?
—SA
Mohibur Rashid 18-Jan-15 0:24am    
your for loop will fail and create error in debug time(if you are using a modern compiler. But if you are using turbo C3.0 you will miss that error). Anyway, you better take a better look at array indexing.

1 solution

It does.
It just doesn't do it when you expect.
Use the debugger; put a breakpoint on the line
C#
for(j=1;j<=c;j++)
And run your code.
Enter your integer and press ENTER and it will hit the breakpoint.
c will be zero, a will be an empty string - because the scanf call will end the integer when it meets the ENTER, but will leave it on the input buffer. So when you try to read the string, there is a empty line "sitting there" waiting for you, and it returns that.
Try this:
C++
int main()
{
    int c,cas,i,j,sum=0;
    char a[100];
    scanf("%d\n", &cas);
    for(i=1;i<=cas;i++)
And it should do what you expected.

Can I suggest that the if code would be a lot more readable is you used a switch statement instead of if?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900