trying to creat a program that reads characters one at a time. An array is created to hold Unique characters. If a character already has been entered into the array, it is not entered again. My problem with this is in the for loop. The way i have the condition set up is so that is checks the partially filled array but also checks the next element which is never filled.
Anyway, Here's what its supposed to do:
Enter up to 20 unique characters:
The authors are Jeri Hanly and Elliot D. Koffman
output: TheautorsJiHnlydED.K
I have two files - a driver and a function to be called - here is the code:
MAIN function
Code:
#define SIZE 20
void add_char(char A[], char ch, int *count);
main()
{
char character;
char ary[SIZE];
int count;
int i;
printf("Enter up to 20 unique characters; "
"quit with 2 control-Ds:\n");
scanf(" %c", &character);
count = 0;
while(count < SIZE)
{
add_char(ary, character, &count);
scanf(" %c", &character);
}
printf("\n");
for (i = 0; i < count; ++i)
{
printf("%c", ary[i]);
}
printf("\n\n");
}
ADD_CHAR function
Code:
void add_char(char A[], char ch, int *count)
{
int i, add;
add = 0;
printf("test for char: %c \n", ch);
for(i = 0; i <= *count; ++i)
{
if (A[i] == ch)
{
}
else
{
add = 1;
}
}
if (add == 1)
{
A[*count] = ch;
++(*count);
}
}