Поиск буквы или строки в C - PullRequest
0 голосов
/ 13 марта 2019

Я очень новичок в программировании на C.У меня обширный опыт работы с другими языками веб-разработки.Я посмотрел простую онлайн игру чисел, скопировал и вставил.Чтобы проверить мое понимание, я также прокомментировал некоторые вещи.Я попытался отладить и понял, что при вводе строки весь код разрывается.Мне просто нужен быстрый ответ на что-то очень простое: как найти буквы в строке.

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

/*Attempting to optimize number game in C*/
// immediate bug: runs FOREVER if text is entered


int main(void) {
    srand(time(NULL)); //I do not know 
    int r = rand() % 10 + 1; //Random number
    int correct = 0; //number correct from guess
    int guess; // Instance of guess
    int counter = 0; // Counter; how many tries
    const char* characters["abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"];// this is my var/*
    int c = 0, count[26] = { 0 }, x;
    */

    char string = [100];
    printf("Guess my number! "); // Does not need to repeat because of conditional statements below...

    do { //Runs at least once... [
        scanf("%d", &guess); // Takes in a number %d = int  
/*
        while (string[c] != '\0') {
            if (string[c] >= 'a' && string[c] <= 'z') {
                x = string[c] - 'a';
                count[x]++
            }

            c++;
        }
*/
        if (guess ==)
        if()

        if (guess == r) {  // Check if true 
            counter++; // Increment counter by 1
            printf("You guessed correctly in %d tries! Congratulations!\n", counter); // 
            correct = 1;
        }

        if (guess < r) { // check if less than
            counter++;
            printf("Your guess is too low. Guess again. ");
        }

        if (guess > r) { // check if more than 
            counter++;
            printf("Your guess is too high. Guess again. ");
        }
    } while (correct == 0); // If correct is null. ]

    return 0;
}

1 Ответ

0 голосов
/ 13 марта 2019

Стандартная библиотека C имеет много функций для работы со строками, таких как strcpy, strncpy, strlen, strchr, strcat и так далее.Вы можете найти буквы в строке, используя функцию 'strchr'.Например:

#include <stdio.h>
#include <string.h>
int main()
{
   const char *astring="Stack Owerflow"; 
   char *ptr = strchr(astring, 'O');
   printf("Found:%c on position: %d\n",*ptr, ptr-astring);
}

/ * Вывод: Найдено: O в положении: 6 * /

...