-
adding ASCII values
how do you add ASCII values?
what i mean by this is that you add the ASCII values of the characters entered.
if the user enters "Hello", the ASCII values are "72, 101, 108, 108, 111".
The function hash is supposed to add those values together. the total should be 500
void hash ( char string[] )
{
int count ;
for( count = 0 ; string[count] != 0 ; count++ )
{
printf( "\n##%c", string[count] ) ;
}
}
this function is supposed to add the ascii values of the characters in string...
-
You're almost there, look at this:
Code:
void hash(char mystring[])
{
int count, total;
for (count = 0, total = 0; mystring[count] != 0; count++)
{
total += mystring[count];
printf("\n##%c", mystring[count]);
}
printf ("total is %d\n", total);
}
Note that I changed the variable string to mystring, as string is reserved.
[edit]
Or a more compact version:
Code:
void hash(char *mystring)
{
int total = 0;
while (*mystring)
total += *mystring++;
printf ("total is %d\n", total);
}