Question #329388

Write a program to take input your name. Now write a function to print your name



in reverse order (eg. RAM KUMAR becomes RAMUK MAR). Then Write one more



function to find sum of ASCII values of all the characters (including space) in your



name using function.

Expert's answer

#include <stdio.h>
#include <string.h>


/* Print a string in reverse oreder */
void print_reverse(char* str) {
    int n, i;

    n = strlen(str);
    for (i=n-1; i>=0; i--) {
        putchar(str[i]);
    }
    putchar('\n');
}

/* Return the sum of ASCII values of all the characters in the string */
int sum_ascii(char* str) {
    int n, i;
    int sum = 0;

    n = strlen(str);
    for (i=0; i<n; i++) {
        sum += str[i];
    }

    return sum;
}

int main() {
    char name[100];
    int sum;

    printf("Enter your name: ");
    gets(name);

    printf("Your name in reverse order:\n");
    print_reverse(name);

    sum = sum_ascii(name);
    printf("\nSum of ASCII values of all the characters in your name is %d\n", sum);

    return 0;
}
LATEST TUTORIALS
APPROVED BY CLIENTS