任何人都可以找到坏词块程序中的错误.. [英] Can anyone find the bug in bad words block program..

查看:51
本文介绍了任何人都可以找到坏词块程序中的错误..的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该程序是为了读取一个坏词并阻止除了第一个和最后一个字符之外的所有字符



我尝试了什么:



the program is meant to read a bad word and block all characters except the first and last character

What I have tried:

#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
int change(char , int);
void star(char ,int);
void main()
{
    int i,y,j,c,w;
    char str[100],word[1000];
    clrscr();
    cout<<" enter text"<<endl;
    gets(word);
    cout<<"The string before block"<<endl;
    puts(word);
    cout<<"The string after block";
    for(i=0;str[i]!='\0';++i)
    {
        if( word[i]!=' ')
        {
            str[j] = word[i];
            j++;
        }
        if(word[i]==' ')
        {
            y=strlen(str);
            w=change(str,c);
            if(w==1)
            {
                star(str,y);
                cout<<str<<" ";
                j=0;
            }
            if(w==0)
            {
                cout<<str<<" ";
                j=0;
            }
        }
    }
    getch();
}

int change(char str[],int c)
{

    if(str=='poop'||'balls')
        return 1;
    else return 0;
}
void star(char str[],int y)
{  
    int i;
    for(i=1;i<y-1;++i)>
    {
        str[i]="*";
     }
}

推荐答案

嗯...首先查看你的代码:如果编译了(它不会)我甚至没有运行它就能看到一个问题:

Um...start by looking at your code: if it compiled (which it won't) I can see one problem without even running it:
if(str=='poop'||'balls')



这不符合你的想法:它是两个独立的测试。

第一个测试是 str =='poop',如果是false,则执行第二个测试:'balls'请注意,第二个测试根本没有提到 str 变量:if只查看字符串并确定它是零还是非零。因为它是一个常量字符串,所以它永远不会为零,因此它总是被视为 true

如果条件总是通过,则为Ite。更正引号,以便编译并修复:


That doesn't do what you think it does: it's two separate tests.
The first test is str=='poop' and if that is false it executes the second test: 'balls' Note that the second test doesn;t mention the str variable at all: if just looks at the string and decides if it is zero or nonzero. Since it's a constant string, it will never be zero, so it will always be taken as true
Ite if condition would always pass. Correcting the quotes so it compiles and fixing that:

if(str == "poop"||str == "balls")

会做你想要的,但即使这样它也行不通,因为在这种情况下==比较指针值,而不是字符序列指向。

要使用字符数组,您需要使用strcmp:

would do what you want, but even then it won't work, because == in this case compares pointer values, not the character sequences they point to.
To use char arrays, you would need to use strcmp:

if(strcmp(str, "poop") == 0 ||strcmp(str, "balls") == 0)


除了解决方案1中提到的错误之外还有更多错误:

There are more errors beside those mentioned in solution 1:
for(i=0;str[i]!='\0';++i)



这里您正在访问 str 变量而不进行初始化。当使用高警告级别时,编译器会抱怨它。

你可能想在这里使用字符串。



相同的变量 j 从未初始化。



正确的代码:


Here you are accessing the str variable without having it initialised. The compiler will complain about it when using a high warning level.
You probably want to use the word string here instead.

Same for the variable j which is never initialised.

Correct code:

int i,y,c,w;
int j = 0;
char str[100],word[1000];
str[0] = '\0';
// ...
for(i=0;word[i]!='\0';++i)







下一个问题是这一行:




The next problem is in this line:

y=strlen(str);



strlen 函数要求传递的字符串以NULL结尾。但是你的字符串不是NULL终止的。你可以自己计算长度(当附加字符时)或者将下面的一个设置为NULL,而不是使用 strlen


The strlen function requires that the passed string is NULL terminated. But your string is not NULL terminated. Instead of using strlen you can count the length yourself (when appending characters) or set the following one to NULL:

{
    if( word[i]!=' ')
    {
        str[j] = word[i];
        j++;
        // Terminate string here or use j as count (current length)
        str[j] = '\0';
    }



请注意,您的更改函数也会受到影响(字符串也必须为NULL终止比较)。因此,确保它始终为NULL终止将是最佳解决方案。


Note that your change function is affected too (the string must be also NULL terminated for comparison). So ensuring that it is always NULL terminated would be the best solution.


这篇关于任何人都可以找到坏词块程序中的错误..的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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