HiveBrain v1.2.0
Get Started
← Back to all entries
patterncMinor

Inputting and displaying strings

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
andstringsinputtingdisplaying

Problem

I have written some code for one of my assignments. However, I feel that I am repeating myself slightly in a few places. I have that niggling feeling that there is a better way to do things.

Here is the code in full:

#include 
#include 
#define ARR_SIZE 256

int main(void)
{
    //Declare char arrays
    char arcInputString5[ARR_SIZE];
    char arcInputString10[ARR_SIZE];
    char arcInputString15[ARR_SIZE];
    char arcInputString20[ARR_SIZE];
    int clean1, clean2, clean3, clean4, nCount;
    char buffer[ARR_SIZE];

    printf("\nPlease Input String 1 - Max Length 5: ");
    //gets(arcInputString5);
    fgets(arcInputString5, ARR_SIZE, stdin);

    for(clean1 = 0; clean1  5)
    {
        printf("\n\nString Length: %d - Exceeded Allowed Characters", strlen(arcInputString5));
    }
    else
    {
        printf("\n\nString Length: %d - NOT Exceeded Allowed Characters", strlen(arcInputString5));
    }

    if(strlen(arcInputString10) > 10)
    {
        printf("\n\nString Length: %d - Exceeded Allowed Characters", strlen(arcInputString10));
    }
    else
    {
        printf("\n\nString Length: %d - NOT Exceeded Allowed Characters", strlen(arcInputString10));
    }

    if(strlen(arcInputString15) > 15)
    {
        printf("\n\nString Length: %d - Exceeded Allowed Characters", strlen(arcInputString15));
    }
    else
    {
        printf("\n\nString Length: %d - NOT Exceeded Allowed Characters", strlen(arcInputString15));
    }

    if(strlen(arcInputString20) > 20)
    {
        printf("\n\nString Length: %d - Exceeded Allowed Characters", strlen(arcInputString20));
    }
    else
    {
        printf("\n\nString Length: %d - NOT Exceeded Allowed Characters", strlen(arcInputString20));
    }

    //printf("\n\nBelow are the strings Concatenated: \n");
    sprintf(buffer, "\n\nBelow are the strings Concatenated: \n\n>%s<>%s>%s<", arcInputString5, arcInputString10, arcInputString15, arcInputString20);
    printf("%s", buffer);
    //puts(buffer)
}


I

Solution

You currently have separate variables for each input. You could instead have a two-dimensional array:

char inputs[4][ARR_SIZE];


You have variables clean1, clean2, …, nCount, but only use clean1. Since they're only used as index variables in non-nested loops, you only need one.

Then you can refactor the reading parts to have a for loop that reads each input in turn. You can do something similar when printing the strings out.

For checking their lengths, you can also do something similar, but you might want to have an array of maximum lengths to check against, e.g.:

static const int max_lengths[] = { 5, 10, 15, 20 };

Code Snippets

char inputs[4][ARR_SIZE];
static const int max_lengths[] = { 5, 10, 15, 20 };

Context

StackExchange Code Review Q#39779, answer score: 6

Revisions (0)

No revisions yet.