I was writing a function for finding character from a given string:
Test.c file is as follows:
#include<stdio.h>
int findIndex(char *array, char search)
{
    int count=0;
    char test;
    int check = 0, repeat;
    if(search == ',')
        repeat = rand()%10;
    else
        repeat = rand()%11;
    while((test = array[count]) != NULL)
    {
        if(test == search)
        {
            if(check == repeat)
                return count;
          check++;
        }
        count++;
    }
}
int main()
{
    char sc[] = "cn:Y,x509UniqueIdentifier:Y,pseudonym:Y,name:Y,l:Y,street:Y,state:Y,postalAddress:Y,postalCode:Y,telephoneNumber:Y,emailAddress:Y";
    char testchar;  
    printf("Enter the search character: ");
    testchar = getc(stdin);
    printf("The search char found at: %d position.\n",findIndex(sc,testchar));
    fflush(stdin);
    while(testchar != 'N')
    {
        printf("The search char found at: %d posotion.\n",findIndex(sc,testchar));
        printf("Enter the search character: ");
        scanf("%c",&testchar);
                fflush(stdin);
    }        
    return 0;
}
The expected output was the index of character only one time, but I am getting this:-
[amarjeet@amarjeet ~]$ ./a.out 
Enter the search character: Y
The search char found at: 66 position.
The search char found at: 128 position.
Enter the search character: The search char found at: 0 position.
Enter the search character:
Where did I go wrong? Please let me know what is the issue and how to solve it.
 
     
     
     
    