Page 8 of 8
Runtime error help me on 494
Posted: Mon Jul 22, 2013 9:42 pm
by arafat13
My code:
Code: Select all
import java.util.*;
import java.lang.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str;
while(true)
{
str = sc.nextLine();
System.out.println(countWords(str));
}
}
public static int countWords(String s)
{
int counter = 0;
boolean word = false;
int endOfLine = s.length() - 1;
for (int i = 0; i < s.length(); i++)
{
if (Character.isLetter(s.charAt(i)) == true && i != endOfLine)
{
word = true;
}
else if (Character.isLetter(s.charAt(i)) == false && word == true)
{
counter++;
word = false;
}
else if (Character.isLetter(s.charAt(i)) && i == endOfLine)
{
counter++;
}
}
return counter;
}
}
Re: Runtime error help me on 494
Posted: Tue Jul 23, 2013 12:49 am
by brianfry713
On the sample input your code throws this:
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Scanner.java:1585)
at Main.main(Main.java:17)
You need to check if there is another line before:
str = sc.nextLine();
Re: Runtime error help me on 494
Posted: Tue Jul 23, 2013 9:05 am
by arafat13
thanks a lot. Ac by using only
while(sc.hasNext())
{
str = sc.nextLine();
System.out.println(countWords(str));
}
Re: WA please help me..
Posted: Fri Jul 26, 2013 12:20 pm
by raihan_sust5
i'm getting WA in the code below..bt don't know about the problems...help me please...
Code: Select all
#include<stdio.h>
#include<ctype.h>
#include<string.h>
int main()
{
int count, strln, word, n = 1;
char str[300000];
while(n <1000000)
{
scanf(" %[^\n]", str);
word =count = 0;
strln = strlen(str);
while (str[count] != '\0')
{
str[count] = toupper(str[count]);
++count;
}
if (strln == 1 || strln == 0)
printf("0\n");
else
{
for (count = 0; count < strln; ++count)
{
if (str[count] >= 'A' && str[count] <= 'Z')
{
while (str[count] >= 'A' && str[count] <= 'Z')
++count;
// printf("%d\n", count);
++word;
}
// printf("%d\n", count);
}
printf("%d\n", word);
//}
++n;
}
}
return 0;
}
Re: Runtime error help me on 494
Posted: Sat Jul 27, 2013 3:31 am
by brianfry713
Your code doesn't terminate after the last line of input.
Re: why WA in 494?!!
Posted: Fri May 09, 2014 5:05 pm
by muktadir.rhmn
Code: Select all
#include<stdio.h>
int main()
{
char ch;
int whitespace = 1, count = 0;
while( ( ch = getchar() ) != EOF){
if(whitespace && ( ( 'a' <= ch && ch <='z') || ( 'A' <= ch && ch <='Z'))){
count++;
whitespace = 0;
}
else if( ch == ' ' || ch =='\t' ){
whitespace = 1;
}
else if ( ch == '\n' ){
printf("%d\n", count);
count = 0;
whitespace = 1;
}
}
return 0;
}
why WA? plz help
Re: why WA in 494?!!
Posted: Fri May 09, 2014 8:02 pm
by brianfry713
Re: why WA in 494?!!
Posted: Sat May 10, 2014 7:28 am
by muktadir.rhmn
thnx
Re: why WA in 494?!!
Posted: Sat May 10, 2014 10:14 pm
by pnkj_agrl
why giving wrong answer for the last input pls help
here's my code
Code: Select all
#include<cstdio>
#include<cstring>
int main()
{
char a[1000005];
int count,i,len;
while(gets(a))
{
len=strlen(a);
count=0;
for(i=0;i<=len;i++)
{
if(a[i]==' ')
count++;
}
printf("%d",count+1);
printf("\n");
}
return 0;
}
Re: why WA in 494?!!
Posted: Mon May 12, 2014 10:20 pm
by brianfry713
A "word'' is defined as a consecutive sequence of letters (upper and/or lower case). You can't assume words will only be separated by a single space.
WA in 494 !! but i don't understand why !!
Posted: Wed Jun 04, 2014 2:12 pm
by saif14
Code: Select all
#include<iostream>
#include<cstring>
#include<string>
using namespace std;
int main()
{
string s,s1,s2;
while(getline(cin,s))
{
char *cstr = new char[s.length()+1];
strcpy(cstr,s.c_str());
int i,j=0;
for(i=0;; i++)
{
s1=cstr[i];
//cout<<cstr[i]<<endl;
if(cstr[i]=='\0')
break;
if(ispunct(cstr[i]))
{
s1=" ";
}
s2=s2+s1;
}
char *ncstr = new char[s2.length()+1];
strcpy(ncstr,s2.c_str());
char *p= strtok(ncstr," ");
j=0;
while(p!=0)
{
j++;
p = strtok(NULL," ");
}
cout<<j<<endl;
delete[] cstr;
delete[] ncstr;
s2.resize(0);
}
return 0;
}
Re: WA in 494 !! but i don't understand why !!
Posted: Wed Jun 11, 2014 10:26 pm
by brianfry713
Input a1a output should be 2
Re: 494 - Kindergarten Counting Game
Posted: Wed Nov 19, 2014 8:48 pm
by Xenon Kfr
why i get time limit in this code ?
Code: Select all
#include<stdio.h>
int main()
{
char c[1000];
int count,nc,i;
count=0;nc=1;
while((gets(c))!=EOF)
{
for(i=0;c[i];i++)
{
if( ( (c[i]>64 && c[i]<91) || (c[i]>96 && c[i]<123) ) )
{
if(nc==1)
{
count++;
nc=0;
}
}
else
nc=1;
}
printf("%d\n",count);
count=0;nc=1;
}
return 0;
}
and why i get runtime error in this code ???
Code: Select all
#include<stdio.h>
int main()
{
char c;
int count,nc;
count=0;nc=1;
while((scanf("%c",&c))!=EOF)
{
if(c=='\n')
{
printf("%d\n",count);
count=0;nc=1;
}
if( ( (c>64 && c<91) || (c>96 && c<123) ) )
{
if(nc==1)
{
count++;
nc=0;
}
}
else
{
nc=1;
}
}
}
Re: 494 - Kindergarten Counting Game
Posted: Wed Nov 19, 2014 10:29 pm
by brianfry713
For your first code: gets doesn't return EOF (-1), it will return NULL (0) at the end of stdin.
http://www.cplusplus.com/reference/cstdio/gets/
Try adding return 0 at the end of your second code.
Instead of:
while((scanf("%c",&c))!=EOF)
it is safer to write:
while((scanf("%c", &c)) == 1)
http://www.cplusplus.com/reference/cstdio/scanf/
Re: 494 - Kindergarten Counting Game
Posted: Fri Nov 21, 2014 12:49 am
by Xenon Kfr