Have a C program that I cannot seem to get fully correct any help is appericated
ID: 3820961 • Letter: H
Question
Have a C program that I cannot seem to get fully correct any help is appericated.
I cannot seem to get the last parts done.
My fix capitaliztion also appears not to work correctly
My code
#include<stdio.h>
#include <string.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=0,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];
}
}
}*/
int n = strlen(usrStr);
while (i < n)
{
if(usrStr[i]==' ' && (usrStr[i+1]==' ' || usrStr[i-1]==' '))
{
for(j=i;j<n;j++)
usrStr[j]=usrStr[j+1];
n--;
}
else
{
i++;
}
}
}
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
#include<stdio.h>
#include <string.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,j;
for (i = 0; i < strlen(usrStr); ++i) {
int j=i;
if ((usrStr[i] == '.')){
for(j=i+1; j<strlen(usrStr);j++){// loop to find a alphabet after the .(period)
if(isalpha(usrStr[j]) != 0){
usrStr[j] = toupper(usrStr[j]);
break;// to break from the inner loop
}
}
/*if(usrStr[j])
(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=0,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];
}
}
}*/
int n = strlen(usrStr);
while (i < n)
{
if(usrStr[i]==' ' && (usrStr[i+1]==' ' || usrStr[i-1]==' '))
{
for(j=i;j<n;j++)
usrStr[j]=usrStr[j+1];
n--;
}
else
{
i++;
}
}
}
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;
}
------output--------------
Enter a sample text:
i want some water. he has some. may be he can give me some.
You entered: i want some water. he has some. may be he can give me some.
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:
f
Edited text: I want some water. He has some. May be he can give me some.
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:
f
Edited text: I want some water. He has some. May be he can give me some.
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:
s
Edited text: I want some water. He has some. May be he can give me some.
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:
q
---------------output--------------
Note: I have updated the fixCaptilization method to properly upper case the first letter of the first owrd of the sentence.
Feel free to ask any question. Happy to help. God bless you!!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.