494 - Kindergarten Counting Game
Moderator: Board moderators
Still not working...
I changed the line
cur_word.clear();
by
cur_word = ''";
It compiled and worked fine in my pc under g++ and cygwin, but i'm still getting CE... any suggestions?!
Thanks!
cur_word.clear();
by
cur_word = ''";
It compiled and worked fine in my pc under g++ and cygwin, but i'm still getting CE... any suggestions?!
Thanks!
You used 2..
I believe you removed only one of them. Remove both of them. I m sure that you will not get CE.
Hope it helps.
Code: Select all
cur_word.clear();
Hope it helps.
Ami ekhono shopno dekhi...
HomePage
HomePage
Still CE... :(
Hi!
I posted the following code, but I'm still getting CE...
I changed the lines you told me but.. it's not getting accepted... what else an be wrong?
Thank you
I posted the following code, but I'm still getting CE...
I changed the lines you told me but.. it's not getting accepted... what else an be wrong?
Thank you
Code: Select all
#include <string>
#include <iostream>
#include <ctype.h>
//#include <iomanip>
using namespace std;
int main()
{
string linha, cur_word;
int count, char_count;
while(getline(cin, linha))
{
count = 0;
char_count = 0;
cur_word = "";
while(char_count < linha.size())
{
while(isalpha(linha[char_count]) && char_count < linha.size())
{
cur_word += linha[char_count];
char_count++;
}
for(int i = 0; i < cur_word.size(); i++)
if(isalpha(cur_word[i]))
{
count++;
break;
}
cur_world = "";
char_count++;
}
cout<<count<<endl;
}
return 0;
}
Very careless typo in your code.
You declared the following variables in your code:
but further down in your code I see this:
You declared the following variables in your code:
Code: Select all
string linha, cur_word;
Code: Select all
cur_world = "";
How could I be so STUPID?!
You we're right....
I was too stupid, im sorry for bothering u all!
Ang I Got AC! Thanks!
I was too stupid, im sorry for bothering u all!
Ang I Got AC! Thanks!
-
- New poster
- Posts: 37
- Joined: Fri Apr 30, 2004 6:52 pm
- Location: Portugal
At first glance your program doesn't give the correct output if the line starts with a character that's is not in A-Z and a-z like ponctuation and whitespace.
For example:
You program would output 6 wich is wrong.
Also you program didn't output the correct values for the example given because of this condition:
gets function fills the array only until the last character in str position, the rest of the array has garbage for so saying so you should have while (i < strlen(str)) instead.
Good luck, you're near the correct answer
For example:
Code: Select all
.. This is a valid input!
Also you program didn't output the correct values for the example given because of this condition:
Code: Select all
while (str[i]!='\0')
Good luck, you're near the correct answer

Did you even test your code with the sample input. Your code gives wrong answer with the sample input.
Change this:
to this:
This will make your code pass the sample input but your code will still give WA if the sample input contains one word like this:
Input:
a
Correct Output:
1
But your code gives:
0
Change this:
Code: Select all
while(!isalpha(str[i])) i++;
to this:
Code: Select all
while(str[i] != '\0' && !isalpha(str[i])) i++;
Input:
a
Correct Output:
1
But your code gives:
0
I have not read your code. Try submitting 2 or 3 times. I think you will get it Accepted in 0.000 seconds. Otherwise try to avoid printf. Then your runtime will decrease.
Ami ekhono shopno dekhi...
HomePage
HomePage
well anyways ..... i checked on the web and according to C standards gets must terminate the string with a '\0' character. so if i use
then it should have worked properly i think ????
Code: Select all
while(str[i]!='\0')
It should, depending on the value of i (I didn't get a chance to read your codeabhi wrote:well anyways ..... i checked on the web and according to C standards gets must terminate the string with a '\0' character. so if i usethen it should have worked properly i think ????Code: Select all
while(str[i]!='\0')

You had two while loops in your original code. The first one did this:
while(str != '\0')
This is fine of course.
It is the inner while loop that can cause problems:
while(!isalpha(str)) i++;
If str is '\0' in this loop your code will still increment i, since the null character is not an alphabet character according to the isalpha function.
while(str != '\0')
This is fine of course.
It is the inner while loop that can cause problems:
while(!isalpha(str)) i++;
If str is '\0' in this loop your code will still increment i, since the null character is not an alphabet character according to the isalpha function.
494 - WA ?
var a:array [0..100] of char;count,i:integer;
function ileslow(s:string):integer;
var k:integer;var slowo_start:boolean;counter:longint;
begin
slowo_start:=false;
counter:=0;
for k:=1 to length(s) do
if (s[k] in ['a'..'z']) or (s[k] in ['A'..'Z']) then
begin
if not slowo_start then
begin
inc(counter); slowo_start:=true;
end;
end else
slowo_start:=false;
ileslow:=counter;
end;
begin
count:=0;
while true do
begin
readln(a[count]);inc(count);
if (a[count-1][length(a[count-1])]=#26) or (a[count-1][1]=#26) then break;
end;
for i:=0 to count-1 do
writeln(ileslow(a));
end.
I don't understand what is wrong
function ileslow(s:string):integer;
var k:integer;var slowo_start:boolean;counter:longint;
begin
slowo_start:=false;
counter:=0;
for k:=1 to length(s) do
if (s[k] in ['a'..'z']) or (s[k] in ['A'..'Z']) then
begin
if not slowo_start then
begin
inc(counter); slowo_start:=true;
end;
end else
slowo_start:=false;
ileslow:=counter;
end;
begin
count:=0;
while true do
begin
readln(a[count]);inc(count);
if (a[count-1][length(a[count-1])]=#26) or (a[count-1][1]=#26) then break;
end;
for i:=0 to count-1 do
writeln(ileslow(a));
end.
I don't understand what is wrong

reply to 494(fixit)
Hi dear fixit
First:you didn't say what kind of problem you had with your code.After submition what kind of error you recived.
Your algorithm for this problem is true for sure.I submitted a solution for this problem by this algoritm and I recived an ACCEPTED reply.
Here is my code in c++:
-----------------------------------------------
#include<iostream.h>
int counter(char str[]){
int count=0;
bool inword=false;
int i=0;
for(;str;i++)
if((str>='a' && str<='z')||(str>='A' && str<='Z')){
if(inword==false){
inword=true;
count++;
}
}
else inword=false;
return count;
}
void main(){
char str[100000];
while(cin.getline(str,100000))
cout<<counter(str)<<endl;
}
-------------------------------------------------------------------------
Try to correct your own code.
I saw some mistakes in your code:
1)You recived all of inputs and saved them and then solved each of them.
while you can read and solve them one by one why you did so?
2)I think the size of array a(100) is too small.
3)You define array a so: a:array [0..100] of char;
but you use it in a different model: a[count-1][length(a[count-1])]
(I mean the number of brakets[])
All of the above are my opinions and maybe they are wrong,but I hope I was helpful.
And at the end I excuse if I couldn't express myself good.English isn't my
tongue languge and I'm not good at it.
with the best regards
First:you didn't say what kind of problem you had with your code.After submition what kind of error you recived.
Your algorithm for this problem is true for sure.I submitted a solution for this problem by this algoritm and I recived an ACCEPTED reply.
Here is my code in c++:
-----------------------------------------------
#include<iostream.h>
int counter(char str[]){
int count=0;
bool inword=false;
int i=0;
for(;str;i++)
if((str>='a' && str<='z')||(str>='A' && str<='Z')){
if(inword==false){
inword=true;
count++;
}
}
else inword=false;
return count;
}
void main(){
char str[100000];
while(cin.getline(str,100000))
cout<<counter(str)<<endl;
}
-------------------------------------------------------------------------
Try to correct your own code.
I saw some mistakes in your code:
1)You recived all of inputs and saved them and then solved each of them.
while you can read and solve them one by one why you did so?
2)I think the size of array a(100) is too small.
3)You define array a so: a:array [0..100] of char;
but you use it in a different model: a[count-1][length(a[count-1])]
(I mean the number of brakets[])
All of the above are my opinions and maybe they are wrong,but I hope I was helpful.
And at the end I excuse if I couldn't express myself good.English isn't my
tongue languge and I'm not good at it.
with the best regards