Question: 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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!