Answer to Question #329388 in C for Samman

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.

1
Expert's answer
2022-04-16T06:47:49-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS