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 <stdio.h>
#include <string.h>
#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 < strlen(arcInputString5); clean1++)
{
if(arcInputString5[clean1] == '\n' || arcInputString5[clean1] == '\r')
{
arcInputString5[clean1] = '\0';
break;
}
}
printf("\nPlease Input String 2 - Max Length 10: ");
//gets(arcInputString10);
fgets(arcInputString10, ARR_SIZE, stdin);
for(clean1 = 0; clean1 < strlen(arcInputString10); clean1++)
{
if(arcInputString10[clean1] == '\n' || arcInputString10[clean1] == '\r')
{
arcInputString10[clean1] = '\0';
break;
}
}
printf("\nPlease Input String 3 - Max Length 15: ");
//gets(arcInputString15);
fgets(arcInputString15, ARR_SIZE, stdin);
for(clean1 = 0; clean1 < strlen(arcInputString15); clean1++)
{
if(arcInputString15[clean1] == '\n' || arcInputString15[clean1] == '\r')
{
arcInputString15[clean1] = '\0';
break;
}
}
printf("\nPlease Input String 4 - Max Length 20: ");
//gets(arcInputString20);
fgets(arcInputString20, ARR_SIZE, stdin);
for(clean1 = 0; clean1 < strlen(arcInputString20); clean1++)
{
if(arcInputString20[clean1] == '\n' || arcInputString20[clean1] == '\r')
{
arcInputString20[clean1] = '\0';
break;
}
}
printf("\nThankyou For Your Inputs - They Are Shown Back To You Below\n\n");
puts(arcInputString5);
puts(arcInputString10);
puts(arcInputString15);
puts(arcInputString20);
printf("\nThe String Lengths For Each Input Are Listed Below. \nIt is also indicated below if you have typed too many characters in");
printf("\n%d", strlen(arcInputString5));
printf("\n%d", strlen(arcInputString10));
printf("\n%d", strlen(arcInputString15));
printf("\n%d", strlen(arcInputString20));
if(strlen(arcInputString5) > 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<>%s<", arcInputString5, arcInputString10, arcInputString15, arcInputString20);
printf("%s", buffer);
//puts(buffer)
}
I feel that the 4 sections of this code:
for(clean1 = 0; clean1 < strlen(arcInputString20); clean1++)
{
if(arcInputString20[clean1] == '\n' || arcInputString20[clean1] == '\r')
{
arcInputString20[clean1] = '\0';
break;
}
}
And this code:
if(strlen(arcInputString5) > 5)
{
printf("\n\nString Length: %d - Exceeded Allowed Characters", strlen(arcInputString5));
}
else
{
printf("\n\nString Length: %d - NOT Exceeded Allowed Characters", strlen(arcInputString5));
}
Could be shortened somehow, but I am unsure how to do this. They may not be able to, but that's why I'm posting here!
Any hints/tips are appreciated.