C Programming: alternative way of creating a test string and letting the user search for the character within the string vs source code

Viewed 162

This lab is trying to show the use of coder-defined functions to execute the code, but I'm trying to do it alternatively so when we actually are tested on it I won't be freaking out that I just copied the source code.

#define NotFound -1
#define WordSize 20

int stringSearch(char * string, char array, int * letter);

int main(void)
{
    char * string = (char *) malloc(WordSize * sizeof(char));
    char tester = '\0';
    int index_tester = 0, i;

    // do
    // {
    //     printf("Enter a test string and character, enter q for the test string to exit.\n");

    //     printf("Test string: ");
    //     scanf("%s", string);
    //     while (getchar() != '\n') {}                                        
    //     if (strcmp(string, "q") == 0) {                                     
    //         break;
    //     }
    // } // ----> Is it possible to do a while or for look instead? loop here? 
    printf("What is the test string you wish to enter: ?");
    for (i = 0; i < sizeof(string); i++)
    {
        {
        scanf("%c", &string[i]);
        }
    }
    string[i] = '\0';
    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 (int i = 0; i < strlen(string); i++)
        {
            if (string[i] == array)
            {
                *letter = i;
                return (Found);
            }

        }
        return(NotFound);
}

When executing the code, I can put in the string, which I think is working fine, but it will automatically put in the search for some random letters immediately without prompting for the user input. I'm still a greenhorn to all this coding stuff so sorry in advance, any advice would be appreciated though

2 Answers

Apart from the issues pointed out in the comments there is some things you should improve:

  • char * string = (char *) malloc(WordSize * sizeof(char)); is the same as char * string = malloc(WordSize), but for a 20 word string we will need char * string = malloc(WordSize + 1)

  • This part of the code:

    for (i = 0; i < Wordsize; i++) // already corrected
    {
        {
        scanf("%c", &string[i]);
        }
    }
    string[i] = '\0';

This will obligate you to always have a 19 character string. The cycle will not end until you do (you replace the 20th character with the null-terminator).

You can replace the whole thing with:

fgets(string, WordSize + 1, stdin);

And for good measure, discard the extra characters when the input is too big to fit the string.

int c;
while((c = fgetc(stdin)) !='\n' && c =! EOF); //discard until newline, for completion check for EOF return

This will allow a 20 character max size string but also for smaller ones.

Working sample

You should add required headers, use fgets() rather than scanf() and set tester_index to -1 rather than 0 which means found at index 0.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define Found 1
#define NotFound 0
#define WordSize 20

int stringSearch(char * string, char array, int * letter);

int main(void)
{
    char * string = (char *) malloc(WordSize * sizeof(char));
    //char tester = '\0';
    char tester[2] = {'\0', '\0'};
    int index_tester = -1; // 0 means found @ index 0

/* (...) */

    printf("What is the test string you wish to enter: ?\n");

    fgets(string, WordSize, stdin);
    if (string[WordSize-1]=='\n')
        string[WordSize-1]='\0';
    puts(string);

    printf("Tester for the inputed string: \n");
    while (getchar() != '\n') {}
    ///scanf("%c", &tester[0]);
    fgets(tester, 2, stdin);

    int ResultofSearch = stringSearch(string, tester[0], &index_tester);

    if (ResultofSearch == NotFound)
    {
        printf("That letter is not found in the string.\n");
    }
    else {
            printf("Character found at index %d.\n\n", index_tester);
        }

    return 0;
}

int stringSearch(char * string, char c, int * index)
{ ... }

It's definitely not perfect but works more less expected way.

Related