As a learning exercise, I am creating a program that converts characters entered through argv[1]
to integers. The program then finds the digital mean of the integers.
For example: abc
would be 123
, and 1+2+3 = 6
, six being the digital mean. The first part of the program works, but I am unable to properly code the portion that finds the digital mean.
Output from word abc
should be 123 6
; instead it is 123 150
.
EDIT: Solved!
#include <stdio.h>
#include <strings.h>
int main(int argc, char *argv[])
{
char stra[27], strb[0];
int str_num, str_len;
int final = 0;
if (argv[1][0] < 'a')
{
printf("!Argument missing!");
return 0;
}
strcpy(stra, argv[1]);
str_len = strlen(stra);
for (str_num = 0; str_num < str_len; str_num++)
{
if (stra[str_num] <= 'z' && stra[str_num] >= 'a')
{
strb[str_num] = (stra[str_num] - 'a' + 1) % 9 + '0';
if (strb[str_num] == '0')
{
strb[str_num] = '9';
}
}
else
{
printf("%s !Please use only the lower case!", stra);
return 0;
}
}
for (str_num = 0; str_num < str_len; str_num++)
{
final += strb[str_num] - '0';
}
printf("%s %i", strb, final);
return 0;
}