This program is in C. I have most of the problem done however cannot figure out
ID: 3804871 • Letter: T
Question
This program is in C. I have most of the problem done however cannot figure out the last two errors that i am getting.
Any help is appericated. My code and errors are below.
Errors
My code
#include<stdio.h>
#include <string.h>
#include <ctype.h>
int GetNumOfNonWSCharacters(const char usrStr[]) {
int num = 0,i;
for (i = 0; i < strlen(usrStr); ++i) {
if ((usrStr[i] == ' ') || (usrStr[i] == ' ') || (usrStr[i] == ' ') || (usrStr[i] == '')) {
} else {
++num;
}
}
return num;
}
int GetNumOfWords(const char usrStr[]) {
int num = 1, i;
for ( i = 0; i < strlen(usrStr); ++i) {
if ((usrStr[i] == ' ') && (usrStr[i + 1] != ' ')) {
++num;
}
}
return num;
}
void FixCapitalization(char usrStr[]) {
usrStr[0] = toupper(usrStr[0]);
int i;
for (i = 0; i < strlen(usrStr); ++i) {
if ((usrStr[i] == '.') && (isalpha(usrStr[i + 3]) != 0)) {
usrStr[i + 3] = toupper(usrStr[i + 3]);
}
}
}
void ReplaceExclamation(char usrStr[]) {
int i = 0;
for (i = 0; i < strlen(usrStr); ++i) {
if ((usrStr[i] == '!')) {
usrStr[i] = '.';
}
}
}
void ShortenSpace(char usrStr[]) {
int i,j;
for (i = 0; i < strlen(usrStr); ++i) {
if ((usrStr[i] == ' ') && (usrStr[i + 1] == ' ')) {
for (j = i; j < strlen(usrStr); ++j) {
usrStr[j+1] = usrStr[j+2];
}
}
}
}
char PrintMenu(char usrStr[]) {
char menuOp = ' ';
printf("MENU ");
printf("c - Number of non-whitespace characters ");
printf("w - Number of words ");
printf("f - Fix capitalization ");
printf("r - Replace all !'s ");
printf("s - Shorten spaces ");
printf("q - Quit ");
while (menuOp != 'c' && menuOp != 'w' && menuOp != 'f' &&
menuOp != 'r' && menuOp != 's' && menuOp != 'o' &&
menuOp != 'q') {
printf("Choose an option: ");
scanf(" %c", &menuOp);
}
if (menuOp == 'c') {
printf("Number of non-whitespace characters: %d ", GetNumOfNonWSCharacters(usrStr));
menuOp = ' ';
} else if (menuOp == 'w') {
printf("Number of words: %d ", GetNumOfWords(usrStr));
menuOp = ' ';
} else if (menuOp == 'f') {
FixCapitalization(usrStr);
printf("Edited text: %s ", usrStr);
menuOp = ' ';
} else if (menuOp == 'r') {
ReplaceExclamation(usrStr);
printf("Edited text: %s ", usrStr);
menuOp = ' ';
} else if (menuOp == 's') {
ShortenSpace(usrStr);
printf("Edited text: %s ", usrStr);
menuOp = ' ';
}
return menuOp;
}
int main() {
char userString[256];
char menuChoice = ' ';
printf("Enter a sample text: ");
fgets(userString, 256, stdin);
printf(" ");
printf("You entered: %s ", userString);
while (menuChoice != 'q') {
menuChoice = PrintMenu(userString);
}
return 0;
}
Explanation / Answer
I wrote this complete code for you, so that if any module that troubles you, you can simply copy that from the given solution.
#include<stdio.h>
#define SIZE 200
void main() {
char *string, printMenu(char *), option;
string = malloc(sizeof(char)*SIZE);
gets(string);
do {
option = printMenu(string);
}while (option!='q' && option!='Q');
}
char printMenu(char* input) {
int GetNumOfNonWSCharacters(const char*), GetNumOfWords(char*);
void FixCapitalization(char *), ReplaceExclamation(char *), ShortenSpace(char *);
char option;
printf("MENU c - Number of non-whitespace characters w - Number of words f - Fix capitalization r - Replace all !'s s - Shorten spaces q - Quit Choose an option: ");//printing the menu
option = getchar(); //input from standard input
switch(option){
case 'c':
printf("Number of non WS chars: %d ",GetNumOfNonWSCharacters(input));
break;
case 'w':
printf("number of words: %d ",GetNumOfWords(input));
break;
case 'f':
FixCapitalization(input);
printf("Edited string: %s ", input);
break;
case 'r':
ReplaceExclamation(input);
printf("Edited string: %s ", input);
break;
case 's':
ShortenSpace(input);
printf("Edited string: %s ", input);
break;
}
return option;
}
int GetNumOfNonWSCharacters(const char* input) {
int i, count = 0;
for (i = 0; input[i]!=''; i++)
if(input[i]!=' ' && input[i]!=' ' && input[i]!=' ' && input[i]!=' ') //checks for WS
count ++;
return count;
}
int GetNumOfWords(char *input) {
int i, count = 1;
for (i = 0; input[i]!=''; i++)
if(input[i]==' ' || input[i]==' ') //Checks for blanks or tab
count ++; // and count them
return count;
}
void FixCapitalization(char *input) {
int i, flag = 1; //flag is set when a new sentence is starting
for (i = 0; input[i]!=''; i++)
if(input[i]==' ' || input[i]==' ') //ignore blank spaces
continue;
else if(input[i]=='.' || input[i]=='!' || input[i]=='?') //if end of sentence mark it
flag = 1;
else if (flag){
flag = 0; //once we find the first charecter we know that end of sentence effect ends
if (input[i] >='a' && input[i] <='z') //if it is end of sentence and char is lower
input[i] -= 32; //change it to upper case
}
}
void ReplaceExclamation(char *input){ //this method is very simple
int i;
for (i = 0; input[i]!=''; i++)
if(input[i]=='!')
input[i]='.';
}
void ShortenSpace(char *input){
int i, j, flag = 0; //flag here marks a blank space is seen
for (i = 0; input[i]!=''; i++)
if (flag == 1 && (input[i]==' ' || input[i] == ' ')) { //if flag is set and we encounter another blank space we shift the arrray left
for (j = i; input[j]!=''; j++)
input[j] = input[j+1];
i--; //since the array is shorten, we need to change value of i
}
else if(input[i]==' ' || input[i]==' ')
flag = 1; //mark blank space
else
flag = 0; //rset flag if it is not blank space
}
I have checked the code with few examples and hope there is no bug in the code.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.