鱼C论坛

 找回密码
 立即注册
查看: 3754|回复: 3

C中循环有问题

[复制链接]
发表于 2022-3-31 17:31:35 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#include<stdio.h>  //Include header file stdio.h
int main()//Main function main
{
        char word[20];//define string word
        char target;
        char number;
        int i=0;//define integer i
        int j=0;

         printf("\n------------------------\n");
         printf("WELCOME TO THE SCRAMBLER\n");
         printf("------------------------\n");
         printf("Please enter your string:");
         gets(word);//get strings
         printf("\nWord is : %s\n",word);//out put word
         printf("\nTarget :");
         scanf("%c",&target)//get the target string from the keyboard
         getchar();//make the program wait
         
while(j<100)//loop
{
        if(target=='q')//check if there are target characters
  {
          
        printf("SCRAMBLED STRING IS :%s",word);
           return 0;
  }
          else{       
           printf("Replace with:");
           scanf("%c",&number);       
           getchar();
           printf("\nReplacing %c with %c",target,number);
           getchar();
           i=0;
   while(word[i]!='\0')//loop all the string
      {
            if(word[i]==target)//find the same string
    {
             word[i]=number;//replace target string to number
    }
        i++;//i=i+1
  
   }
   printf("Word is : %s\n",word);
   printf("\nTarget :");
   scanf("%c",&target);
        }
        j++;
}
return 0;
}


大家好,我在做一个可以扫描替换的程序,我发现它第一遍可以,但是当它开始第二轮时无法成功。
想了很久都不行,还请大佬们指点一下
file:///C:/Users/%E6%9D%8E%E4%BA%9A%E8%BD%A9/Desktop/QQ%E6%88%AA%E5%9B%BE20220331172722.png
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-3-31 17:45:09 | 显示全部楼层
你这个B装的我给你100分。
  1. #include<stdio.h>  //Include header file stdio.h
  2. int main()//Main function main
  3. {
  4.         char word[20];//define string word
  5.         char target;
  6.         char number;
  7.         int i=0;//define integer i
  8.         int j=0;

  9.          printf("\n------------------------\n");
  10.          printf("WELCOME TO THE SCRAMBLER\n");
  11.          printf("------------------------\n");
  12.          printf("Please enter your string:");
  13.          gets(word);//get strings
  14.          printf("\nWord is : %s\n",word);//out put word
  15.          printf("\nTarget :");
  16.          scanf("%c",&target);
  17.          while(getchar()!='\n');//make the program wait
  18.          
  19. while(j<100)//loop
  20. {
  21.         if(target=='q')//check if there are target characters
  22.   {
  23.          
  24.         printf("SCRAMBLED STRING IS :%s",word);
  25.            return 0;
  26.   }
  27.           else{      
  28.            printf("Replace with:");
  29.            scanf("%c",&number);      
  30.            while(getchar()!='\n');//make the program wait
  31.            printf("\nReplacing %c with %c\n",target,number);

  32.            i=0;
  33.    while(word[i]!='\0')//loop all the string
  34.       {
  35.             if(word[i]==target)//find the same string
  36.     {
  37.              word[i]=number;//replace target string to number
  38.     }
  39.         i++;//i=i+1
  40.   
  41.    }
  42.    printf("Word is : %s\n",word);
  43.    printf("\nTarget :");
  44.    scanf("%c",&target);
  45.     while(getchar()!='\n');//make the program wait
  46.         }
  47.         j++;
  48. }
  49. return 0;
  50. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-4-10 09:57:30 | 显示全部楼层

  1. #include<stdio.h>  //Include header file stdio.h
  2. int main()//Main function main
  3. {
  4.         char word[20];//define string word
  5.         char target;
  6.         char number;
  7.         int i=0;//define integer i
  8.         int j=0;

  9.          printf("\n------------------------\n");
  10.          printf("WELCOME TO THE SCRAMBLER\n");
  11.          printf("------------------------\n");
  12.          printf("Please enter your string:");
  13.          gets(word);//get strings     //这个没用到{:10_269:}
  14.          printf("\nWord is : %s\n",word);//out put word
  15.          printf("\nTarget :");
  16.          scanf("%c",&target)//get the target string from the keyboard
  17.          getchar();//make the program wait
  18.          
  19. while(j<100)//loop
  20. {
  21.         if(target=='q')//check if there are target characters
  22.   {
  23.          
  24.         printf("SCRAMBLED STRING IS :%s",word);
  25.            return 0;
  26.   }
  27.           else{      
  28.            printf("Replace with:");
  29.            scanf("%c",&number);      
  30.            getchar();
  31.            printf("\nReplacing %c with %c",target,number);
  32.            getchar();
  33.            i=0;
  34.    while(word!='\0')//loop all the string
  35.       {
  36.             if(word==target)//find the same string
  37.     {
  38.              word=number;//replace target string to number
  39.     }
  40.         i++;//i=i+1
  41.   
  42.    }
  43.    printf("Word is : %s\n",word);
  44.    printf("\nTarget :");
  45.    scanf("%c",&target);
  46.         }
  47.         j++;
  48. }
  49. return 0;
  50. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-4-10 09:58:07 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-24 13:35

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表