494 - Kindergarten Counting Game
Moderator: Board moderators
at last solve 494
hi everybody at last i solve this problem.i only change char t[30000].
-
- New poster
- Posts: 8
- Joined: Thu Dec 04, 2003 4:56 pm
[494] whats wrong?
Code: Select all
#include <iostream>
using namespace std;
char buff;
int words;
bool is;
int main() {
while(!cin.eof()) {
buff = cin.get();
while(buff!='\n' && buff!=EOF) {
if( ( buff >= 'a' && buff <= 'z') || (buff >= 'A' && buff <= 'Z') ) {
if(!is) {
is = true;
words++;
}
}
else is = false;
buff = cin.get();
}
cout << words << endl;
words = 0;
is = false;
}
return 0;
}
The last line of the input will look something like:
Another possible problem is that you don't initialize the "words" variable to anything. In theory, it could be just some junk variable, though C++ normally sets everything to 0 for you, it's always better to initialize your own variables and not let the compiler do it.
Currently, your program will still parse the EOF, and thus output an extraneous "0".word1 word2(\n)
(EOF)
Another possible problem is that you don't initialize the "words" variable to anything. In theory, it could be just some junk variable, though C++ normally sets everything to 0 for you, it's always better to initialize your own variables and not let the compiler do it.
-
- New poster
- Posts: 8
- Joined: Thu Dec 04, 2003 4:56 pm
-
- New poster
- Posts: 9
- Joined: Fri Jan 21, 2005 5:09 pm
494-Kindergarten Counting Game: got WA for such an easy one!
For using getlint(cin, ) it does not run properly in VC++
I used mingw.
I define a word as a consecutive sequence of charecter if its each charecter is a letter (upper/lower). Thats what i can do in computer.
I think the problem is there how I define words.
How can I know that a consecutive sequence of cheraters is an word?
I used mingw.
I define a word as a consecutive sequence of charecter if its each charecter is a letter (upper/lower). Thats what i can do in computer.
I think the problem is there how I define words.
How can I know that a consecutive sequence of cheraters is an word?
Code: Select all
#include<iostream>
#include<string>
#include<cstring>
using std::string;
using std::cout;
using std::endl;
using std::cin;
using std::getline;
string ip;
int p1,p2;
int count;
void word_check(string word)
{
char lc=word[word.length()-1]; //lc=last character.
if(word.length()!=1)
if(lc=='.'||lc=='?'||lc=='!')
word.erase(word.length()-1,1); //I just ignore the last character if its length is >1
int f=1;
for(int c1=0;c1<word.length();c1++)
if( !((word[c1]>='A'&&word[c1]<='Z') || (word[c1]>='a'&&word[c1]<='z')) )
{f=0;break;} //I defined word which has all its character between A->z or a->z
if(f)count++;
}
int main()
{
while(getline(cin,ip))
{
count=0;
p1=p2=0;
while(1)
{
while(p1<ip.length()&&ip[p1]==' ')p1++;p2=p1;
if(p1>=ip.length())break;
while(p2<ip.length()&&ip[p2]!=' ')p2++;
word_check(ip.substr(p1,p2-p1));
p1=p2;
}
cout<<count<<endl;
}
return 0;
}
WA (494) please help me to get the reason......
I have considered tab also. But yet it gets WA. Please help me finding out the reason for WA>>>
Code: Select all
/*BEGIN_OF_THE_SOURCE_CODE*/
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#define N 10000
int main()
{
char str[N];
int i,len,word = 0,j;
while(gets(str) != NULL)
{
word = 0;
len = strlen(str);
for(i = 0;i<len;i++)
{
if( isalpha(str[i]) )
{
for(j = i+1;j<len;j++)
{
if(str[j] == ' ' || str[j] =='\t')
{
word++;
break;
}
}
if(j == len )
word++;
i = j;
}
}
printf("%d\n",word);
}
return 0;
}
/*END_OF_THE_SOURCE_CODE*/
Your code fails tests like these:
The output should be:
Code: Select all
abc@def
000a1b2c3d010
ch33se
Code: Select all
2
4
2
494 WA
Dear ACM members,
Could you please take a look at my code and tell me what's wrong because I am getting WA all the time and I read all the posts about it on the forum and it works properly with the example inputs. Thank you
I hope somebody will be able to help me out. Thx 
Best regards,
Refik
Could you please take a look at my code and tell me what's wrong because I am getting WA all the time and I read all the posts about it on the forum and it works properly with the example inputs. Thank you
Code: Select all
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main()
{
string line1="";
long long int duzina=0;
long long int count = 0;
while(getline(cin, line1))
{
duzina= 0;
count = 0;
line1= " " + line1 + " ";
for(long long int i=0; i<line1.length(); i++)
{
if(ispunct(line1[i])!=0 || isspace(line1[i])!=0 || isdigit(line1[i])!=0 || isblank(line1[i])!=0 || iscntrl(line1[i])!=0)
{
string word = "";
word = line1.substr(duzina, i-duzina);
if(isalpha(word[0]))
{
count++;
}
duzina=i+1;
}
}
cout << count <<endl;
};
return 0;
};

Best regards,
Refik
consider this algo
read string
if(isalpha(string[0]))count++;
and than in some loop until the one before last element of string(excluding '\0')
if( (isspace(string) && isalpha(string[i+1])) || (!isspace(string) && !isalpha(string)) && isalpha(string[i+1]) ) count++;
and you dont have to use long long( i used just int )
Hope it helps!
read string
if(isalpha(string[0]))count++;
and than in some loop until the one before last element of string(excluding '\0')
if( (isspace(string) && isalpha(string[i+1])) || (!isspace(string) && !isalpha(string)) && isalpha(string[i+1]) ) count++;
and you dont have to use long long( i used just int )
Hope it helps!
keep it real!
WA 494
need some Test case
Code: Select all
/*
Name: kindergarden countiog
Number: 494
Type : adhoc
Process : ON
Author :Salman Zaman
Email : zamansalman@gmail.com
Date : 21/08/05 23:27
*/
#include<stdio.h>
//#include<string.h>
//#include<conio.h>
int main(){
char ch;
int flag,count;
// freopen("494.txt","r",stdin);
flag=0;
count=0;
while(scanf("%c",&ch)!=EOF){
if((ch>='A' && ch<='Z' )||(ch>='a' && ch<='z')){
flag++;
}
else if(ch=='\n'){
if(flag>0){
count++;
}
printf("%d\n",count);
flag=0;
count=0;
}
else if(ch==' '){
if(flag>0){
count++;
}
flag=0;
}
}
//getch();
return 0;
}
494 WA...
It's ok with sample input...however it can't work when I submit 
---
#include <stdio.h>
int main() {
int space=0;
char a;
while (scanf("%c",&a)==1){
if (a==' '){
scanf("%c",&a);
if (a!='.')
space = space + 1;
}
if (a=='\n'){
printf ("%d\n",space+1);
space = 0;
}
}
return 0;
}

---
#include <stdio.h>
int main() {
int space=0;
char a;
while (scanf("%c",&a)==1){
if (a==' '){
scanf("%c",&a);
if (a!='.')
space = space + 1;
}
if (a=='\n'){
printf ("%d\n",space+1);
space = 0;
}
}
return 0;
}
494 - Compile Error
Hi everybody!
I don't know why it's getting CE, it compiles normally with g++ at cygwin...
Please take a look!
Thanks, my code follows
I don't know why it's getting CE, it compiles normally with g++ at cygwin...
Please take a look!
Thanks, my code follows

Code: Select all
#include <string>
#include <iostream>
#include <cctype>
//#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.clear();
while(char_count < linha.size())
{
while(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_word.clear();
char_count++;
}
cout<<count<<endl;
}
return 0;
}
Remove the following function...
Hope it works.
Code: Select all
cur_word.clear();
Ami ekhono shopno dekhi...
HomePage
HomePage