Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help with array 1

Status
Not open for further replies.

cell0042

Programmer
Nov 5, 2002
16
US
The program I'm trying to write has a value for each letter in the alphabet. When someone enters a word I want to be able to total each letter in the word up and show a total value for the word. I was wondering if i was on the right track. I'm just trying to test it out and no matter what letter I put in I get the same answer of -64. This is what I have so far.

#include <stdio.h>

int main()
{
char word[100];

int a = 1, b = 2, c = 3, d = 4, e = 5, f = 6, g = 7,
h = 8, i = 9, j = 10, k = 11, l = 12, m = 13, n = 14, o = 15;

printf(&quot;Enter a word\n&quot;);
scanf(&quot;%s&quot;, word);
if (word[100] != 'NULL')

printf(&quot; is %d\n&quot;, word[100]);

return 0;


}
 
Sorry but just about every part of your code is wrong...

try [tt]if (word != NULL)[/tt] instead of [tt]if (word[100] != 'NULL')[/tt] and also take off the [tt][100][/tt] from inside the final printf statement.

That being said, it still won't give you what you want...

Forget about declaring a load of ints for each letter. Each item (character) in a string array such as [tt]word[/tt] already has an ASCII value. The best bet would be to make the string all lower case (for simplicitiy) and then subtract 96 from each character's value.
Then, you will have a=1,b=2 and so on by default.

Then, you need to access each character in the array, one by one, and total up the numeric value.

[tt]#include <string.h>
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
char word[100] = &quot;&quot;;
char output[100] = &quot;&quot;;
char temp[100] = &quot;&quot;;
unsigned calc = 0;

printf(&quot;Enter a word\n&quot;);
scanf(&quot;%s&quot;, word);
if (word != NULL)
{
_strlwr(word); // convert to lowercase

// loop through each character in word
for (unsigned x=0;x<strlen(word);x++)
{
// convert ASCII value-96 to a string
sprintf(temp,&quot;%u&quot;,(unsigned)word[x]-96);
// tack this number/string onto output string
strcat(output,temp);
}

printf(output);
printf(&quot;\n\n&quot;);
}

return (0);
}[/tt]


now, if you enter 'abc' the output is '123' - is what you want?

[rockband]
tellis.gif

[sup]programmer (prog'ram'er), n A hot-headed, anorak wearing, pimple-faced computer geek.[/sup]​
 
He probably wants to get the sum of all the numeric values. e.g. if you enter &quot;acb&quot;, it outputs 6 (1+2+3).

In this case, do the same thing, but make an int initialized to 0 and add the value to it each time through the loop. Then display that at the end.

Note: _strlwr is nonstandard; if your library doesn't support it, you'll have to use another loop and tolower all the characters in the array.
 
yes... that's actually what the [tt]unsigned calc = 0;[/tt] was for in my earlier post but I neglected to incorporate that part of the code - sorry!
Here it is:

[tt]#include <string.h>
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
char word[100] = &quot;&quot;;
char output[100] = &quot;&quot;;
char temp[100] = &quot;&quot;;
unsigned calc = 0;

printf(&quot;Enter a word\n&quot;);
scanf(&quot;%s&quot;, word);
if (word != NULL)
{
_strlwr(word); // convert to lowercase

// loop through each character in word
for (unsigned x=0;x<strlen(word);x++)
{
// convert ASCII value-96 to a string
sprintf(temp,&quot;%u&quot;,(unsigned)word[x]-96);
// tack this number/string onto output string
strcat(output,temp);
}

printf(output);
printf(&quot;\n\n&quot;);

/** OUTPUT SUM TOTAL **/
for (unsigned z=0;z<strlen(word);z++)
{
calc += word[z]-96;
}

// convert number to string
sprintf(output,&quot;%u&quot;,calc);
printf(output);
printf(&quot;\n\n&quot;);
}

return (0);
}[/tt]

if enter 'abc' first output would be '123' and second output is '6'

[rockband]
tellis.gif

[sup]programmer (prog'ram'er), n A hot-headed, anorak wearing, pimple-faced computer geek.[/sup]​
 
yes... that's actually what the [tt]unsigned calc = 0;[/tt] was for in my earlier post but I neglected to incorporate that part of the code - sorry!
Here it is:

[tt]#include <string.h>
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
char word[100] = &quot;&quot;;
char output[100] = &quot;&quot;;
char temp[100] = &quot;&quot;;
unsigned calc = 0;

printf(&quot;Enter a word\n&quot;);
scanf(&quot;%s&quot;, word);
if (word != NULL)
{
_strlwr(word); // convert to lowercase

// loop through each character in word
for (unsigned x=0;x<strlen(word);x++)
{
// convert ASCII value-96 to a string
sprintf(temp,&quot;%u&quot;,(unsigned)word[x]-96);
// tack this number/string onto output string
strcat(output,temp);
}

printf(output);
printf(&quot;\n\n&quot;);

/** OUTPUT SUM TOTAL **/
for (unsigned z=0;z<strlen(word);z++)
{
calc += word[z]-96;
}

// convert number to string
sprintf(output,&quot;%u&quot;,calc);
printf(output);
printf(&quot;\n\n&quot;);
}

return (0);
}[/tt]

if enter 'abc' first output would be '123' and second output is '6'


[rockband]
tellis.gif

[sup]programmer (prog'ram'er), n A hot-headed, anorak wearing, pimple-faced computer geek.[/sup]​
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top