鱼C论坛

 找回密码
 立即注册
查看: 3214|回复: 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
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-3-31 17:45:09 | 显示全部楼层
你这个B装的我给你100分。
#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);
         while(getchar()!='\n');//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);       
           while(getchar()!='\n');//make the program wait
           printf("\nReplacing %c with %c\n",target,number);

           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);
    while(getchar()!='\n');//make the program wait
        }
        j++;
}
return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-4-10 09:57:30 | 显示全部楼层
#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!='\0')//loop all the string
      {
            if(word==target)//find the same string
    {
             word=number;//replace target string to number
    }
        i++;//i=i+1
  
   }
   printf("Word is : %s\n",word);
   printf("\nTarget :");
   scanf("%c",&target);
        }
        j++;
}
return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-4-10 09:58:07 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-10-7 04:29

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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