I am having issues with this program. I seem to not find what is wrong. I am try
ID: 3596142 • Letter: I
Question
I am having issues with this program. I seem to not find what is wrong. I am trying to get it to do these things:
Read a string and compute the summation of each character ASCII code
It will keep track of the sum of the characters for each word
Track the smallest and the largest token summation
Program:
#include <stdio.h>
#include <string.h>
int main()
{
char strText[100] ="Start";
int nTextLen = strlen(strText);
int count = 0;
printf("Welcome to token generator! ");
printf("Enter a word to use in the token generator. You may enter as many words as you like. Press q and <Enter> key when finished. ");
while((strlen(strText) ! = 1) || (strText[0] ! = 'q')) {
//compute when to stop loop
//check nTextLen == 1 and strText[0] == 'q'
Scanf(%c, &strlen(strText));
count ++;
printf("Current number of token entered %d ", count);
printf("Current token value = %d ", strlen(strText));
printf("Current final token value = %d ", strlen(strText));
}
printf("Thank you for using tokengen!");
} //end function
Explanation / Answer
//header files required
#include <stdio.h>
#include <string.h>
#include <ctype.h>
//main function begin
int main() {
//varible declaration part
char c;
char strText[100];
int ic=0,asciicount=0,max=0,min=10000,token=0;
printf("Welcome to token generator! Enter a word to use in the token generator Press q and <Enter> key when finished. ");
//loop repeats until q entered
while (1)
{
scanf("%c",&c); //reading input char wise
if(c!='q') //if input is q quits input taking
strText[ic]=c;
else
break;
//printf("%d ",strText[ic]); //display each char ascii value if need
asciicount=asciicount+strText[ic]; //sum of ascii value of entire string
token=token+strText[ic];// sum of ascii vlaue is done until space occurs
//check input is space then find max token or min token lenths
if(c==' ')
{
if(token>max)
{
max=token;
}
if(token<min)
{
min=token;
}
token=0;
}
ic++; //loop varible increment
}
//display outputs
printf("ASCII SUM OF STRING %d ", asciicount);
printf("Max token length = %d ", max);
printf("Min token length = %d ", min);
}
Output :
Welcome to token generator!
Enter a word to use in the token generator Press q and <Enter> key when finished.
ashokk kuamr rebba q
ASCII SUM OF STRING 1789
Max token length = 673
Min token length = 540
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.