Question

#include <stdio.h> #include <string.h> #include <ctype.h> #include <stdlib.h> int main(void) { /* Type your code here....

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
int main(void) {

/* Type your code here. */

int GetNumOfNonWSCharacters(const char usrStr[]) {
int length;
int i;
int count = 0;
char c;
length=strlen(usrStr);

for (i = 0; i < length; i++) {
c=usrStr[i];
if ( c!=' ' )
{
count++;
}
}
  
return count;
}


int GetNumOfWords(const char usrStr[]) {
int counted = 0; // result

// state:
const char* it = usrStr;
int inword = 0;

do switch(*it) {
case '\0':
case ' ': case '\t': case '\n': case '\r': // TODO others?
if (inword) { inword = 0; counted++; }
break;
default: inword = 1;
} while(*it++);

return counted;
  
}


void FixCapitalization(char usrStr[]) {
  
int length;
int i;
int count = 0;
char c;
length=strlen(usrStr);

for (i = 0; i < length; i++) {
c=usrStr[i];
if(i==0)
{
usrStr[i]=toupper(usrStr[i]);
}
if ( c=='.')
{
if( usrStr[i+1]!=' ')
{
usrStr[i+1]=toupper(usrStr[i+1]);
}
else
{
usrStr[i+2]=toupper(usrStr[i+2]);
}
}
}
  
printf("%s",usrStr);
}

void ReplaceExclamation(char usrStr[]) {
int i;
for(i=0;usrStr[i]!='\0';i++)
{
if(usrStr[i]=='!')
{
usrStr[i]='.';
}
}
printf("%s",usrStr);
}


void ShortenSpace(char usrStr[]) {
  
int j,length=1,i;
for(i=0;usrStr[i]!='\0';i++)
{
if(usrStr[i]==' ')
{
length=0;
for(j=i+1;usrStr[j]==' ';j++)
{
length++;
  
}
  
if(length!=0)
{
for(j=i+1;usrStr[j]!='\0';j++)
{
usrStr[j]=usrStr[j+length];
}
}
}
}
printf("%s",usrStr);
}

void PrintMenu(char userString[])
{
char choice;
  
do
{
printf("MENU\n");
printf("c - Number of non-whitespace characters\n");
printf("w - Number of words\n");
printf("f - Fix capitalization\n");
printf("r - Replace all !\'s\n");
printf("s - Shorten spaces\n");
printf("q - Quit\n\n");
printf("Choose an option:\n");
scanf("%c",&choice);
  
switch(choice)
{
case 'c' :
printf("Number of non-whitespace characters: %d", GetNumOfNonWSCharacters(userString));
printf("\n");
break;
case 'w' :
printf("Number of words: %d\n\n", GetNumOfWords(userString));
break;

case 'f' :
printf("Edited text: ");
FixCapitalization(userString);
break;
case 'r' :
printf("Edited text: ");
ReplaceExclamation(userString);
break;
case 's' :
printf("Edited text: ");
ShortenSpace(userString);
break;

case 'q' :
  

exit (0);
break;


}
}while(1);
}

int main(); {
char userString[256];
printf("Enter a sample text:");
printf("\n");
scanf ("%[^\n]%*c", userString);
printf("\nYou entered: %s\n",userString);
printf("\n");

PrintMenu(userString);

return 0;
}
}

ERROR; Program generated too much output. Output restricted to 50000 characters. Check program for any unterminated loops generating output.

can someone please help me fix this code

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please use the below code

#include<string.h>

#include <ctype.h>

#include<stdio.h>

void PrintMenu(char[]);

int GetNumOfNonWSCharacters(char[]);

int GetNumOfWords(char[]);

char* FixCapitalization(char[]);

char* ReplaceExclamation(char[]);

char* ShortenSpace(char[]);

int main()

{

char line[300];

printf("Enter a sample text\n");

fgets(line, 300, stdin);

printf("\n");

printf("You entered: \n");

printf("%s\n\n", line);

PrintMenu(line);

}

// function for printing menu each time and captures the user choice

void PrintMenu(char line[])

{

char ch = 'c';

while(1)

{

printf("MENU\n");

printf("c - Number of non-whitespace characters\n");

printf("w - Number of words\n");

printf("f - Fix Capitalization\n");

printf("r - Replace all !'s\n");

printf("s - Shorten spaces\n");

printf("q - Quit\n");

printf("Choose an option: ");

scanf(" %c", &ch);

if (ch == 'c'){

printf("Number of non-white space chars are: %d\n", GetNumOfNonWSCharacters(line));

}

else if(ch == 'w'){

printf("Number of words: %d\n", GetNumOfWords(line));

}

else if(ch == 'f'){

printf("%s\n", FixCapitalization(line));

}

else if(ch == 'r'){

printf("%s\n", ReplaceExclamation(line));

}

else if(ch == 's'){

printf("%s\n", ShortenSpace(line));

}

else if(ch == 'q'){

break;

}

else

printf("Invalid Choice!\n");

printf("\n");

fflush(stdin);

}

}

//getting number of chars excluding the spaces

int GetNumOfNonWSCharacters(char line[])

{

int count=0;

int i;

for( i = 0;line[i] != '\0'; i++){

if(!isspace(line[i]))

count++;

}

return count;

}

//method for number of rows

int GetNumOfWords(char line[])

{

int count=0;

int i;

char* line2 = ShortenSpace(line);

for( i = 0;line2[i] != '\0'; i++){

if(isspace(line2[i]))

count++;

}

return count;

}

// Fixing the capitalization

char* FixCapitalization(char line[])

{

int len = strlen(line);

line[0] = toupper(line[0]);

int ind = 0;

int i;

for( i = len-1; i >= 1; i--)

{

if( !(line[i] == '.' || line[i] == '?' || line[i] == '!') );

{

if(isalpha(line[i]))

ind = i;

}

if(line[i] == '.' || line[i] == '?' || line[i] == '!')

{

line[ind] = toupper(line[ind]);

}

}

return line;

}

// function for replacing the !

char* ReplaceExclamation(char line[])

{

int i;

for( i = 0; line[i] != '\0'; i++)

{

if(line[i] == '!')

line[i]='.';

}

return line;

}

// function to remove spaces

char* ShortenSpace(char line[])

{

static char inputStr[100];

strcpy(inputStr, line);

static char outputStr[100] = " ";

int inputI = 0;

int outputI = 0;

while(inputStr[inputI] != '\0')

{

outputStr[outputI] = inputStr[inputI];

if(inputStr[inputI] == ' ')

{

while(inputStr[inputI + 1] == ' ')

{

// skip over any extra spaces

inputI++;

}

}

outputI++;

inputI++;

}

// adding a null character

outputStr[outputI] = '\0';

return outputStr;

}

============================================
SEE OUTPUT

Thanks, PLEASE COMMENT if there is any concern.

Add a comment
Know the answer?
Add Answer to:
#include <stdio.h> #include <string.h> #include <ctype.h> #include <stdlib.h> int main(void) { /* Type your code here....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The code keeps saying its generating too much output check for any unterminated loops. I cant...

    The code keeps saying its generating too much output check for any unterminated loops. I cant find any. Can anyone help? #include <iostream> #include <string> using namespace std; char printMenu(); int GetNumOfNonWSCharacters(string); int GetNumOfWords(string); void ReplaceExclamation(string&); void ShortenSpace(string&); int FindText(string, string); int main(){ char option; string text, phraseToFind; cout << "\n\n Enter a sample text: "; getline(cin, text); cout << "\n\n You entered: " << text; while(1){ option = printMenu(); switch(option){ case 'q': case 'Q': return 0; case 'c': case...

  • (1) Prompt the user to enter a string of their choosing. Store the text in a...

    (1) Prompt the user to enter a string of their choosing. Store the text in a string. Output the string. (1 pt) Ex: Enter a sample text: We'll continue our quest in space. There will be more shuttle flights and more shuttle crews and, yes, more volunteers, more civilians, more teachers in space. Nothing ends here; our hopes and our journeys continue! You entered: We'll continue our quest in space. There will be more shuttle flights and more shuttle crews...

  • Finish function to complete code. #include <stdio.h> #include <stdlib.h> #include<string.h> #define Max_Size 20 void push(char S[],...

    Finish function to complete code. #include <stdio.h> #include <stdlib.h> #include<string.h> #define Max_Size 20 void push(char S[], int *p_top, char value); char pop(char S[], int *p_top); void printCurrentStack(char S[], int *p_top); int validation(char infix[], char S[], int *p_top); char *infix2postfix(char infix[], char postfix[], char S[], int *p_top); int precedence(char symbol); int main() { // int choice; int top1=0; //top for S1 stack int top2=0; //top for S2 stack int *p_top1=&top1; int *p_top2=&top2; char infix[]="(2+3)*(4-3)"; //Stores infix string int n=strlen(infix); //length of...

  • Fix the errors in C code #include <stdio.h> #include <stdlib.h> void insertAt(int *, int); void Delete(int...

    Fix the errors in C code #include <stdio.h> #include <stdlib.h> void insertAt(int *, int); void Delete(int *); void replaceAt(int *, int, int); int isEmpty(int *, int); int isFull(int *, int); void removeAt(int *, int); void printList(int *, int); int main() { int *a; int arraySize=0,l=0,loc=0; int choice; while(1) { printf("\n Main Menu"); printf("\n 1.Create list\n 2.Insert element at particular position\n 3.Delete list.\n4. Remove an element at given position \n 5.Replace an element at given position\n 6. Check the size of...

  • #include <stdio.h> #include <stdlib.h> #include <string.h> #include<ctype.h> #define MAX_LEN 255 int numWords(char *str); int numDigit(char *str);...

    #include <stdio.h> #include <stdlib.h> #include <string.h> #include<ctype.h> #define MAX_LEN 255 int numWords(char *str); int numDigit(char *str); int numUppltr(char *str); int numLwrltr(char *str); int punChar(char *str); char*readString(char *str); int main() { char givString[MAX_LEN]; puts("Enter your string(Max 255 characters):"); //readString(givString); gets(givString); printf("\nnumber of words :%d",numWords(givString)); printf("\nnumber of uppercase letters %d",numUppltr(givString)); printf("\nnumber of lowercase letters %d",numLwrltr(givString)); printf("\nnumber of punctuations %d\n",punChar(givString)); printf("\nnumber of digits:%d\n",numDigit(givString)); system("pause"); return 0; } char *readString(char *str) { int ch, i=0; while((ch=getchar())!=EOF && ch!='\n') { if(i) { str[i]=ch; i++; }...

  • Please explain how these code run for each line #include<stdio.h> #include<string.h> /** * Part A */...

    Please explain how these code run for each line #include<stdio.h> #include<string.h> /** * Part A */ struct myWord{    char Word[21];    int Length; }; int tokenizeLine(char line[], struct myWord wordList[]); void printList(struct myWord wordList[], int size); void sortList(struct myWord wordList[], int size); /** * main function */ int main() {    struct myWord wordList[20];    char line[100];    printf("Enter an English Sentence:\n");    gets(line);    int size = tokenizeLine(line, wordList);    printf("\n");    printf("Unsorted word list.\n");    printList(wordList, size);...

  • ​what's wrong with my code?????? #include <stdio.h> #include <stdlib.h> #include <string.h>                         &

    ​what's wrong with my code?????? #include <stdio.h> #include <stdlib.h> #include <string.h>                            #define MAXBINS 99 #define MAXCORS 26 #define MAXCUS 100 #define MAXPUR 10 /* datatype for a list of orders ----------------- */ typedef struct { int bin_order_number; char bin_order_char; }bin_order; typedef struct { int orderNo; bin_order orderbin; }order; typedef struct { int cusnumber; int n;   /* number of the orders what the txt include */ order oo[MAXPUR+1]; }customer; typedef struct{ int n; /*number of the customers */ customer cc[MAXCUS+1];...

  • what is the output of the following program? #include<stdio.h> #include<string.h> int main(void){ char word[20]; int i...

    what is the output of the following program? #include<stdio.h> #include<string.h> int main(void){ char word[20]; int i =0    strcpy(word, "ORGANISE"); while(word[i] !='\0'){ if(i%2 ==1) word[i] = 'C'; i++; } printf("%s",word); return 0; }

  • #include <stdio.h> #include <stdlib.h> #include <strings.h> #include <unistd.h> #include <pthread.h> pthread_mutex_t mtx; // used by each...

    #include <stdio.h> #include <stdlib.h> #include <strings.h> #include <unistd.h> #include <pthread.h> pthread_mutex_t mtx; // used by each of the three threads to prevent other threads from accessing global_sum during their additions int global_sum = 0; typedef struct{ char* word; char* filename; }MyStruct; void *count(void*str) { MyStruct *struc; struc = (MyStruct*)str; const char *myfile = struc->filename; FILE *f; int count=0, j; char buf[50], read[100]; // myfile[strlen(myfile)-1]='\0'; if(!(f=fopen(myfile,"rt"))){ printf("Wrong file name"); } else printf("File opened successfully\n"); for(j=0; fgets(read, 10, f)!=NULL; j++){ if (strcmp(read[j],struc->word)==0)...

  • devmem2.c #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <errno.h> #include <signal.h> #include <fcntl.h> #include...

    devmem2.c #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <errno.h> #include <signal.h> #include <fcntl.h> #include <ctype.h> #include <termios.h> #include <sys/types.h> #include <sys/mman.h>    #define FATAL do { fprintf(stderr, "Error at line %d, file %s (%d) [%s]\n", \ __LINE__, __FILE__, errno, strerror(errno)); exit(1); } while(0) #define MAP_SIZE 4096UL #define MAP_MASK (MAP_SIZE - 1) int main(int argc, char **argv) { int fd; void *map_base = NULL, *virt_addr = NULL; unsigned long read_result, writeval; off_t target; int access_type = 'w';    if(argc...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT