Last active
September 27, 2020 12:03
-
-
Save svagionitis/5718404 to your computer and use it in GitHub Desktop.
[06/4/13] Challenge #128 [Easy] Sum-the-Digits, Part II - http://www.reddit.com/r/dailyprogrammer/comments/1fnutb/06413_challenge_128_easy_sumthedigits_part_ii/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
(Easy): Sum-the-Digits, Part II (http://www.reddit.com/r/dailyprogrammer/comments/1fnutb/06413_challenge_128_easy_sumthedigits_part_ii/) | |
------------------------------------------------------------------------------------------------------------------------------------------ | |
Given a well-formed (non-empty, fully valid) string of digits, let the integer N be the sum of digits. Then, given this integer N, turn it into a string of digits. Repeat this process until you only have one digit left. Simple, clean, and easy: focus on writing this as cleanly as possible in your preferred programming language. | |
`Author: nint22. This challenge is particularly easy, so don't worry about looking for crazy corner-cases or weird exceptions. This challenge is as up-front as it gets :-) Good luck, have fun!` | |
Formal Inputs & Outputs | |
----------------------- | |
### Input Description ### | |
On standard console input, you will be given a string of digits. This string will not be of zero-length and will be guaranteed well-formed (will always have digits, and nothing else, in the string). | |
### Output Description ### | |
You must take the given string, sum the digits, and then convert this sum to a string and print it out onto standard console. Then, you must repeat this process again and again until you only have one digit left. | |
Sample Inputs & Outputs | |
----------------------- | |
### Sample Input ### | |
Note: Take from Wikipedia for the sake of keeping things as simple and clear as possible. | |
12345 | |
### Sample Output ### | |
12345 | |
15 | |
6 | |
*/ | |
#include <stdlib.h> | |
#include <stdio.h> | |
#include <string.h> | |
#include <ctype.h> | |
void usage(char *argv0); | |
void sumOfDigits(unsigned long *sum, size_t *lenDigitsOut, char *digits); | |
void convertNumberToString(char **stringOut, unsigned long num); | |
int main(int argc, char *argv[]) | |
{ | |
char *str = NULL; | |
unsigned long sum = 0; | |
size_t lenDigits = 0; | |
if (argc != 2) | |
{ | |
usage(argv[0]); | |
exit(1); | |
} | |
sumOfDigits(&sum, &lenDigits, argv[1]); | |
printf("str: %s lenDigits: %lu\n", argv[1], lenDigits); | |
while(lenDigits > 1 ) | |
{ | |
convertNumberToString(&str, sum); | |
sumOfDigits(&sum, &lenDigits, str); | |
printf("str: %s lenDigits: %lu\n", str, lenDigits); | |
} | |
} | |
/** | |
* Print usage infomation in command line. | |
* @param argv0 The name of the executable | |
*/ | |
void usage(char *argv0) | |
{ | |
printf("Usage: %s [string of digits]\n", argv0); | |
return; | |
} | |
/** | |
* Calculate the sum of digits from a string. | |
* @param digits The string which contains the digits | |
* @param lenDigitOut Returns the length of the string | |
* @param sumOut Returns the sum of the digits of the string | |
*/ | |
void sumOfDigits(unsigned long *sumOut, size_t *lenDigitsOut, char *digits) | |
{ | |
*sumOut = 0; | |
*lenDigitsOut = strlen(digits); | |
size_t i = 0; | |
for(i = 0;i<*lenDigitsOut;i++) | |
if (isdigit(digits[i])) /** Check if character is digit. */ | |
*sumOut += (digits[i] - '0'); | |
} | |
/** | |
* Convert an unsigned long number to string | |
* @param num The number to be converted to string | |
* @param stringOut The converted string from the number | |
*/ | |
void convertNumberToString(char **stringOut, unsigned long num) | |
{ | |
/** Free memory if already allocated. */ | |
if (*stringOut != NULL) | |
free(*stringOut); | |
size_t numLen = snprintf(NULL, 0, "%lu", num); | |
if (numLen < 0) | |
return; | |
*stringOut = (char *)malloc(numLen+1); | |
snprintf(*stringOut, numLen+1, "%lu", num); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment