0

Hi Everyone,

I am having some trouble with the code I am writing. I am supposed to find the sum all ASCII codes in the string, omitting the newline character at the end of the user input.

Here are the directions I am given:

Create a program that accepts a string from the user (not from the command line) and displays

1. The number of words in the string (words are separated by characters or tabs)
2. The sum of all ASCII codes in the string, omitting the newline character at the end of the user input
* For example, if the string is "d d" the sum of all ASCII codes is 100 + 32 + 100 = 232.

Input

* A string entered by the user

Output

1. A display of the number of words in the string
2. A display of the sum of all ASCII codes in the string


Enter a string: This is a test
String: This is a test
Words: 4
Sum: 1269

I figured out everything but the sum of the ASCII codes. Any tips, hints, or help would be much appreciated.

Here is my code:

#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
#define N 128

void Wcount(char *str);
void cSum(char *str);

int main()
{
    char userInput[N];
    int size, i, x;
    int mynum;
    
    /*prompts user to enter a string*/
    printf("Please enter a string: ");
    gets(userInput);
    
    /*Prints string entered*/
    printf("String: ");
    puts(userInput);

    /*word count*/
    Wcount(userInput);
       
    /*ASCII sum*/
    cSum(userInput);
    
    system("pause");
    return(0);
}

/*Counts number of words in string*/
void Wcount(char *str)
{   
int j=0, wordCount=0;
	while (str[j] !='\0')
	{
		if( isspace(str[j]) && !isspace(str[j+1]))

			wordCount++;

		j++;
	}
	wordCount++;
	printf("Words: %d\n", wordCount);
}

/*Supposed to sum up the ASCII values, but I can't figure out how*/
void cSum(char *str)
{     
      
     

     
      printf("Sum: %d\n", str);
}
2
Contributors
1
Reply
6
Views
7 Years
Discussion Span
Last Post by wildgoose
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.