#include #include #include // -------------------------------------------------- // Compute the sum of the ascii values of all // the characters in the string int total(char *my_string) { int total = 0; int i = 0; // We don't expect strings to be longer than 10000 characters while ( i < 10000 ) { total += my_string[i]; i++; } return total; } // -------------------------------------------------- // Take a string as input // Return the reversed string char* reverse(char *my_string) { // Let's create the string that we will return and initialize it // to the empty string char *new_string = ""; // Let's iterate over the string that we were given in reverse // and write the output to the new string int src = strlen(my_string)-1; int dst = 0; while ( src > 0 ) { new_string[dst] = my_string[src]; dst++; src--; } // Return the reversed string return new_string; } // -------------------------------------------------- int main(int argc, char* argv[]) { if ( argc < 2 ) { printf("Please enter a string\n"); return 1; } char *string_to_reverse = argv[1]; // Step 1: Compute the sum of all ascii values int sum = total(string_to_reverse); printf("Sum of ascii values is %d\n",sum); // Step 2: Reverse the string char *reversed_string = reverse(string_to_reverse); // Step 3: Calling total on reversed string sum = total(reversed_string); printf("Sum of ascii values is %d\n",sum); printf("Original string %s\n",string_to_reverse); printf("Reversed string %s\n",reversed_string); return 0; }