c - 刽子手编程: unscrambling words (c programming)

标签 c

我编码的时间不长(只有两个月)。 我的问题与循环中计数器的迭代有关。下面是我的程序 与同时:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
int cnt=0;
int match;
int position;
int tries=0;

char letter;


int main()
{FILE *Difficult= fopen("/GODlvl.txt", "r"); 
    char singl[19];
    if (Difficult==NULL){
        return -1;}

    for(int i = 0; i < rnd1; i++)
        fgets(singl, 21, Difficult);

    int DashN1= strlen(singl); 
    printf("%s", singl);
    for (int i=0; i<DashN1-1; i++)
    {
        singl[i]='_';
        singl[DashN1]='\0';
    }

for (int i=0; i<DashN1; i++) /**it adds an extra character ..possibly a space..**/
{
    printf(" ");
    printf(" %c", singl[i]);
}
do{
scanf(" %c", &letter); 
if(letter==singl[cnt])
{
    match=1;
    position=cnt;
    printf("match found");
}
if (position==cnt)
{
    singl[position]=letter;
    printf(" %s", singl);
}
cnt++;
tries++;
}
while(tries!=8);
}

do 循环从 0 开始运行,并在每一步后迭代。问题在于 if 条件;他们不测试 char 数组(singl)中的任何任意元素。我如何编辑此代码(无论是 if 条件还是循环)以针对任意索引运行。

最佳答案

读取letter的用户输入后,您可以请求在singl中使用随机索引,只需执行以下操作:

srand((unsigned)time(NULL));
cnt = rand() % DashN1;

这意味着您的 do 循环应该如下所示:

do{
    scanf(" %c", &letter);
    srand((unsigned)time(NULL));
    cnt = rand() % DashN1;
    if(letter==singl[cnt]){
      //....
    }
}while(/*...*/);

请确保您这样做:

#include<stdlib.h>

为了访问srandrand

关于c - 刽子手编程: unscrambling words (c programming),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53574012/

相关文章:

c - 如何计算字典中单词中前两个字母的出现频率?

c - 有效的 UID 未按预期运行

c - Linux 中 C 中的文件系统

c - 如何找到数组的大小(从指向数组第一个元素的指针)?

c - 用C语言访问一个URL

c - 对于非零 "(a/b)*b + a%b - a",表达式 'b' 在 C 中如何始终为零?

c - "lea"炸弹实验室第 4 阶段命令

c - 初始化 union/结构的正确方法是什么?

正在重新分配的 C 程序指针未分配

ios - 如何在 ios 中定义多个 c 标志?