请告诉程序中的更改以获得所需的结果 [英] Please tell the changes in program to get the desired result

查看:72
本文介绍了请告诉程序中的更改以获得所需的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include<stdio.h>
void main()
    {
    char str[30],*cp,ch;
    printf("enter a string");
    scanf("%s",str);
    char *str1="india";              /*take str as "ilovemyindia"*/
    cp=NULL;
    ch=*str1;
    cp=match(ch,str);
    if(*cp==ch)
        {
        printf("\n");
        while(*cp!='\0')
            cp++;
        printf("%c",*cp);
        }
    else 
        printf("no match found");
    printf("\n");
    }
char*match(char c,char*s)
    {
    while(c!=*s)
        s++;
    return (s);
    }





缩进添加 - OriginalGriff [/ edit]



我尝试过:



显示0错误但未显示所需结果,即匹配是否找到只是要求输入数组然后空格



[edit]Indentation added - OriginalGriff[/edit]

What I have tried:

it is showing 0 error but not showing the desired result i.e match found or not it is just asking to enter array then space

推荐答案

首先缩进你的代码:它使它更容易阅读。

而你至少在学习,永远不要把一行写成一个循环或条件 - 总是使用大括号。它可能看起来很多工作,但是当你改变它或者遇到问题时它会变得更加明显。

所以改变

Start by indenting your code: it make it a lot easier to read.
And while you are learning at least, never write a single line as a loop, or condition - always use curly brackets. It may seem like a lot of work, but it makes thigns a lot more obvious when you change it or get a problem.
So change
while(*cp!='\0')
   cp++;






To

while(*cp!='\0')
   {
   cp++;
   }

等等。相信我,这是值得的!

然后查看你的代码 - 如果你通过调试器运行它会非常明显 - 而且很明显这里有问题。

您注意到的是您从字符串的第一个字符开始:

And so on. Trust me on this, it's worth it!
Then look at your code - and this would be really obvious if you ran it through the debugger - and it's pretty clear there are problems here.
The one you've noticed is that you start with the first char of your string:

ch=*str1;

然后检查它是否在字符串中:

And then check to see if it's in the string:

cp=match(ch,str);

它是什么,因为你只是从那里得到它。然后你仔细检查:

Which it is, because you just got it from there. Then you double check that:

if(*cp==ch)

这将永远是真的!



但你还没有注意到的是你匹配方法真的很讨厌:

which will always be true!

But what you haven't noticed yet is that you match method is really nasty:

char*match(char c,char*s)
    {
    while(c!=*s)
        s++;
    return (s);
    }

如果字符串不包含字符会怎么样?



不仅如此,还是:

What will happen if the string doesn't contain the character?

And not only that, but:

while(*cp!='\0')
    cp++;
printf("%c",*cp);

也不会打印任何东西。永远!



打破调试器,开始逐步完成代码:这是你需要掌握的技能,最好在这样的简单代码示例上学习。

Isn't going to print anything either. Ever!

Break out the debugger, and start stepping through your code: it's a skill you need to master, and it's best learned on simple code examples like this.


引用:

请告诉程序中的更改以获得所需的结果

Please tell the changes in program to get the desired result

您只是忘记了告诉我们什么是实际输出和什么是所需的输出,也不要告诉我们问题或目标是什么。

提出问题是一项技能 [ ^ ]

----------

You only forgot to tell us what is actual output and what is the desired output, nor tell us what is the problem or the goal.
Asking questions is a skill[^]
----------

while(c!=*s)
    s++;



我想你忘了检查字符串的结尾。我猜它总是在变量之外找到匹配。

----------

你应该学会尽快使用调试器可能。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。 />
当代码不做ex的时候你已经接近了一个错误。


I guess you forgot to check for the end of string. I guess that it always find a match somewhere outside of your variables.
----------
You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
When the code don't do what is expected, you are close to a bug.


这篇关于请告诉程序中的更改以获得所需的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆