Click here to Skip to main content
15,890,882 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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]="*";
     }
}
Posted
Updated 18-Jun-16 5:57am
v2

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

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:
C++
if(str == "poop"||str == "balls")
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:
C++
if(strcmp(str, "poop") == 0 ||strcmp(str, "balls") == 0)
 
Share this answer
 
There are more errors beside those mentioned in solution 1:
C#
for(i=0;str[i]!='\0';++i)

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:
C++
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);

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:
C++
{
    if( word[i]!=' ')
    {
        str[j] = word[i];
        j++;
        // Terminate string here or use j as count (current length)
        str[j] = '\0';
    }

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.
 
Share this answer
 
v2
Comments
[no name] 19-Jun-16 2:32am    
error : character constant must be one or two characters long
[no name] 19-Jun-16 2:37am    
should i use

int change(char * , int);
void star(char *,int);
as prototypes? //the compiler forces me to do the same but i cant guess why?

2) if(strcmp(str, "poop") == 0 ||strcmp(str, "balls") == 0)
this was advised.. but should i enclose balls or poop in single quotes or double quotes?

3) error comes when str[i]="*"; why?
Jochen Arndt 19-Jun-16 4:15am    
Single quote for single characters and double quotes for strings (list of characters with a terminating NULL byte).

This answers 2. and 3. (which is an error I oversaw).

To 1:
Another error O oversaw. The declaration on top of the file uses char while the later implementation uses char[]. You must use the same in both places. In your case this is char* or char[].

char is a single character while char* is a pointer to one or more characters like char[] (an array of characters, effectively the same as char*).

A string is a list of characters (char*) where the end is indicated by a NULL byte.
[no name] 19-Jun-16 10:28am    
1)error : character constant must be one or two characters long
2)i cant assign str[i] ="*";

new code

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

int change(char str[])
{

if(strcmp(str,'poop'==0)||strcmp(str,'bad'==0))
return 1;
else return 0;
}
void star(char str[],int y)
{ int i;
for(i=1;i<y-1;++i) {="" str[i]="0;" }="" &nbsp;="" <="" div=""> <div id="EditDialogPlaceholder"></div> <div id="ReplyDialogPlaceholder"></div>
Jochen Arndt 20-Jun-16 3:02am    
str[i] accesses a character
"*" is a string
So it must be:
str[i] = '*';

Also as already noted by Griff:
if(0 == strcmp(str,"poop") || 0 == strcmp(str,"bad"0))

In the above you must use strings. Note also that your conditions (comparing with zero) are placed wrong.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900