Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello! I just have another question about my code. Just need help with debugging. I tried to write a way to error check my code

Hello! I just have another question about my code. Just need help with debugging.

I tried to write a way to error check my code so that if the user enters a string amount that exceeds the defined MAX constant, it would print an error, else the code proceeds like normal. However, it does not seem to be working.

I tried making a function that passes into it the string array and the eSize as on a previous assignment on 2D arrays I used this method to error check for space and it worked. But I am so lost when it comes to this assignment.

I will be posting my solution below.

So all I need is to add an error checking step in my solution so that it will populate an error in the console if the user enters a string with character amount that EXCEEDS the defined Max constant (which determines the size of the array in this situation) if it the user's string is valid then it will proceed with the rest of the code and print out the desired results.

Thank you in advance !!!!!!!!!!!!

My solution:

#include #include #include #include

// defined constant #define MAX 100

// prototypes

void inReverse(char userStr[]); void inVertical(char userStr[]); void inTri(char userStr[]);

// main function

int main(){ char choice; char userStr[MAX] = ""; int i; do{ printf(" Please enter string: "); scanf ("%s", userStr); printf("The string you entered is below: %s ", userStr); printf(" Forward: %s", userStr);

printf(" Backwards: "); inReverse(userStr); printf(" Vertical:" ); inVertical(userStr); printf(" Triangle: "); inTri(userStr);

printf(" "); int length = strlen(userStr); printf(" Just so you know, the length of your string was: %d", length); printf(" Would you like to enter another string? y or n?: "); scanf(" %c", &choice); if (choice == 'y'){ choice = 1; } else { choice = 0; printf("Thank you for using my program."); } } while(choice == 1); return 0; }

// function to reverse string

void inReverse(char userStr[]){ int length = strlen(userStr); int i; for (i = strlen(userStr)-1; i >= 0; i--){ printf("%c", userStr[i]); } }

// function to print string vertically

void inVertical(char userStr[]){ int i; int length= strlen(userStr); for (i = 0; i < strlen(userStr); i++){ printf(" %c ", userStr[i]); } }

// function to print string in Triangle formation

void inTri(char userStr[]){ int i; int j; for ( i = 0; i < strlen(userStr); i++){ for ( j = 0; j <= i; j++) printf("%c ", userStr[j]); printf(" "); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Mysql Examples Explanations Explain Examples

Authors: Harry Baker ,Ray Yao

1st Edition

B0CQK9RN2J, 979-8872176237

More Books

Students also viewed these Databases questions

Question

Prove Theorem11.5.

Answered: 1 week ago