首页 > 解决方案 > 在 C 中打印 *string_variable 缺少什么

问题描述

我正在做一个项目,需要用破折号替换字符串中的字母。我什至无法让字符串打印更少的替换字符。我认为问题在于我对指针缺乏理解(我尚未涵盖)。

我试过迭代,复制到另一个变量和 %s 但没有一个工作......我错过了什么?

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

int main(void) {
  printf("Welcome to the Computer Science Themed Hangman Game!\n The words the you will be asked to guess in this game will be related to computer science.\n");

  char words[15][20] = {"motherboard", "keyboard", "bytes", "programmer", "array", "processor", "loop", "algorithm", "intelligence", "boolean", "compute", "debug", "output", "function", "memory"};

  int i, mistakes = 0;

  int random_word_index = rand() % 15; //selects a random number from 0 to 15

  int word_length = strlen(words[random_word_index]); 
  printf("%d\n", word_length);

  char *selected_word = words[random_word_index];
  char word[20];
  strcpy(word, selected_word);
  printf("%s", word);

  char *current_status;
  printf("%s", current_status);
  
  for (i=0; i<word_length; ++i){
    current_status[i] = '_';
  }
  printf("%s", current_status);

标签: arrayscstring

解决方案


推荐阅读