Junior competition challenge question

Problem J2: Rotating letters
Problem Description
An artist wants to construct a sign whose letters will rotate freely in the breeze. In order to do this,
she must only use letters that are not changed by rotation of 180 degrees: I, O, S, H, Z, X, and N.
Write a program that reads a word and determines whether the word can be used on the sign.
Input Speciļ¬cation
The input will consist of one word, all in uppercase letters, with no spaces. The maximum length
of the word will be 30 letters, and the word will have at least one letter in it.
Output Speciļ¬cation
Output YES if the input word can be used on the sign; otherwise, output NO.
Sample Input 1
SHINS
Output for Sample Input 1
YES
Sample Input 2
NOISE
Output for Sample Input 2
NO


this is my code:

// ConsoleApplication27.cpp :
//
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
string x;
cin>>x;
int z=0;
int l=0;
for(int i=0;i<x.length();i++)

{

if( (int)x[i]=="I"||"O"||"S"||"H"||"Z"||"X"||"N")
{
z++;
}
else
{
l++;
}
if(z>0)
{
cout<<"YES";
}
else{cout<<"NO";}

return 0;
}

tried to compile it but didnt work. Anyone care to tell me how to write the question and how i did it wrong?
Last edited on
if( (int)x[i]=="I"||"O"||"S"||"H"||"Z"||"X"||"N")

is wrong on several levels.

In the expression (int)x[i] == "I" you compare the value of a character in a string to the address of the string literal "I". 'I' is the character.

if (x == 'I' || 'O' ) is equivalent to: if ((x=='I') || 'O') which isn't what you want and will always evaluate to true. You want if (x == 'I' || x == 'O')
first please use code tags
<>
or
[code]//some code[/code]


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// ConsoleApplication27.cpp :
//
#include "stdafx.h" //not really needed you can go under settings and disable precompiled header
#include <iostream>
#include <string>
using namespace std;
int main()
{
    string x;
    cin>>x;
    int z=0; //what is this for?
    int l=0; //I would replace this with a boolean. (preference )
    for(int i=0;i<x.length();i++)

    {

        if( (int)x[i]=="I"||"O"||"S"||"H"||"Z"||"X"||"N") //wrong on multiple levels
        //for starter why cast to int?
        //each letter is a single character not a string so use single quotes
        //you can't chain || operator it must be x[i] == 'I' || x[i] == '0' || ect...
        //shouldn't you check if it is not equal to any then set a bool to false
        {
            z++; //not needed
        }
        else
        {
            l++; //this one you could use instead of boolean
        }
        if(z>0) //it should be if l == 0 .. if they are all valid
        {
            cout<<"YES";
        }
        else
        {
            cout<<"NO";
        }
    } //missing one here.
    return 0;
}


*typo
Last edited on
Thank you very much, but then if i use boolean if the last character was actually an "I" the boolean could become true despite the characters the last character might be a character that is not allowed, which would still make the boolean true if you know what I mean. Any suggestions? or something like const ints that once u set the boolean to false u cant modify it? Im learning c++ all by my own so..
Actually you don't want to set it true or false each time a valid or invalid character is found. You would want to initialize the boolean to true -- it is valid. Then if it finds any invalid ones assign it to false. You can actually stop looping through the letters once it is false to save some processing time to. Though probably will only save micro seconds if that.

Here is an example of what I mean:

1
2
3
4
5
bool valid = true;

for( int i = 0; i < Size; ++i )
    if( /*finds any of the invalid letters*/ )
        valid = false;


You could add in a condition to continue only while valid is true also.

 
for( int i = 0; i < Size && valid; ++i )
looks pretty tacky in a for loop though.
Um how do you add in a condition to continue only while valid is true?
cuz I'm going for the junior challenge and it is important for it to save time.
Also,
<
// ConsoleApplication27.cpp :
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
string x;
cin>>x;
bool valid=true;

for(int i=0;i<x.length();i++)

{

if( x[i]!='I'||'O'||'S'||'H'||'Z'||'X'||'N')
{
valid=false ;
}
}

if(valid=false)
{
cout<<"NO";
}
else
{
cout<<"YES";
}
return 0;
}
>
this code seems to always display "YES". Thank you so much, how should I modify it?
Well once again you are doing your conditions wrong

if( x[i]!='I'||'O'||'S'||'H'||'Z'||'X'||'N')

should be:

if( x[i]!='I'|| x[i] == 'O'|| x[i] == 'S'|| x[i] == 'H'|| x[i] == 'Z'|| x[i] == 'X'|| x[i] == 'N' )

It may look nice to use a switch though:

1
2
3
4
5
6
switch( x[i] )
{
    case 'I': case 'O': case 'S': case 'H': case 'Z': case 'X':
        valid = false;
        break;
}


There is another problem you have

if(valid=false) will never be a true condition. Remember = is for assignment and == is for comparison.

I showed you one example of how to do it earlier:

You could add in a condition to continue only while valid is true also.


for( int i = 0; i < Size && valid; ++i )
looks pretty tacky in a for loop though.


Another way would be like:
1
2
3
4
5
6
7
int i = 0;

while( i < Size && valid )
{
    //check if valid...
    ++i;
}


I would probably use the while loop since for loops are generally for a set amount of times but if you are only looping while its valid that's not necessarily a set amount of times.

Topic archived. No new replies allowed.