#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; while ( i < strlen(my_string) ) { total += my_string[i]; i++; } return total; } // -------------------------------------------------- // Take a string as input // Return the reversed string void reverse(char * new_string, char *my_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--; } new_string[dst] = 0; } // -------------------------------------------------- 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[strlen(string_to_reverse)+1]; reverse(reversed_string,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; }