#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NotFound -1
#define Found 0
#define WordSize 20
int stringSearch(char *string, char array, int *letter);
int main(void)
{
char *string = malloc(WordSize + 1);
char tester = '\0';
int index_tester = 0, c;
printf("What is the test string you wish to enter: ?");
fgets(string, WordSize + 1, stdin);
if (strlen(string) == WordSize && string[WordSize - 1] != '\n')
while ((c = fgetc(stdin)) != '\n' && c != EOF) {} //discard until newline
puts(string);
printf("Tester for the inputed string: ");
scanf(" %c", &tester);
while (getchar() != '\n') {}
int ResultofSearch = stringSearch(string, tester, &index_tester);
if (ResultofSearch == NotFound)
{
printf("That letter is not foudn in the string, try again: ");
}
else
{
printf("Character found at index %d.\n\n", index_tester);
}
return 0;
}
int stringSearch(char *string, char array, int *letter)
{
for (size_t i = 0; i < strlen(string); i++)
{
if (string[i] == array)
{
*letter = i;
return Found;
}
}
return (NotFound);
}