for (i=0; i <= a; i++)
{
if (strlen(a), a[i] == "e" && a[i+1] == "b" && a[i+2] == "a" && a[i+3] == "y")
{
cout <<"Accepted";
}
else
{
cout <<"Not Accepted";
}
}
getch();
}
I'm trying to make a program that accepts only a string containing the words "ebay" and "web", can somebody correct this code I created because it still has errors.
#include <iostream.h> // should be <iostream>
#include <string.h> // should be <string>
#include <conio.h> // don't use it
#include <stdio.h> // don't use it, if you must, include <cstdio>
// new headers require the std:: namespace so add the next line
usingnamespace std;
( If your compiler supports old style headers, it may be quite old and you should get a new one )
if you have a character sequence and you search for contiguous characters, you will find the entire word
eg: a = "hello world ebay!!!" | i |i+1|i+2|i+3| h | e | l | l | o | | w | o | r | l | d | | e | b | a | y | ! | ! | !
Anyway, you can do that by using single quotes: a[i] == 'e'
bool found = false;
for ( int i = 0; i < a.length()-3; i++ ) // a.length()-3 so that a[i+3] won't overflow
{
if ( a[ i ] == 'e' && a[i+1] == 'b' && a[i+2] == 'a' && a[i+3] == 'y' ) // check all digits
{
cout << "Accepted";
found = true; // remember that this was wound
break; // exit from 'for'
}
// if isn't a match continue, it may be found later
}
if ( !found ) // if wasn't found
cout << "Not Accepted";
because I tried to run the program, no output is generated, maybe there is still something wrong:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string a;
cout <<"Enter a word: ";
getline (cin, a);
bool found = false;
for ( int i = 0; i < a.length()-3; i++ ) // a.length() -3 so that a[ i+3 ] won't overflow
{
if ( a[i] == 'e' && a[i+1] == 'b' && a[i+2] == 'a' && a[i+3] == 'y' ) // check all digits
{
cout << "Accepted";
found = true; // remember that this was wound
break; // exit from 'for'
}
// if isn't a match continue, it may be found later
}
if ( !found ) // if wasn't found
cout << "Not Accepted";