494 - Kindergarten Counting Game

All about problems in Volume 4. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

arafat13
New poster
Posts: 2
Joined: Mon Jul 22, 2013 9:37 pm

Runtime error help me on 494

Post 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;
			}


}
Last edited by brianfry713 on Wed Nov 19, 2014 10:17 pm, edited 1 time in total.
Reason: Added code blocks
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: Runtime error help me on 494

Post 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();
Check input and AC output for thousands of problems on uDebug!
arafat13
New poster
Posts: 2
Joined: Mon Jul 22, 2013 9:37 pm

Re: Runtime error help me on 494

Post by arafat13 »

thanks a lot. Ac by using only

while(sc.hasNext())
{
str = sc.nextLine();
System.out.println(countWords(str));
}
raihan_sust5
New poster
Posts: 6
Joined: Tue Jul 23, 2013 3:04 am

Re: WA please help me..

Post 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;

}
Last edited by brianfry713 on Wed Nov 19, 2014 10:18 pm, edited 1 time in total.
Reason: Added code blocks
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: Runtime error help me on 494

Post by brianfry713 »

Your code doesn't terminate after the last line of input.
Check input and AC output for thousands of problems on uDebug!
muktadir.rhmn
New poster
Posts: 6
Joined: Sat May 03, 2014 11:44 am

Re: why WA in 494?!!

Post 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
Last edited by brianfry713 on Wed Nov 19, 2014 10:18 pm, edited 1 time in total.
Reason: Added code blocks
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: why WA in 494?!!

Post by brianfry713 »

Input:

Code: Select all

a.a
Correct output: 2
Check input and AC output for thousands of problems on uDebug!
muktadir.rhmn
New poster
Posts: 6
Joined: Sat May 03, 2014 11:44 am

Re: why WA in 494?!!

Post by muktadir.rhmn »

thnx
pnkj_agrl
New poster
Posts: 2
Joined: Thu May 08, 2014 9:36 pm

Re: why WA in 494?!!

Post 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;
}
Last edited by brianfry713 on Wed Nov 19, 2014 10:18 pm, edited 1 time in total.
Reason: Added code blocks
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: why WA in 494?!!

Post 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.
Check input and AC output for thousands of problems on uDebug!
saif14
New poster
Posts: 1
Joined: Wed Jun 04, 2014 10:02 am

WA in 494 !! but i don't understand why !!

Post 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;
}
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: WA in 494 !! but i don't understand why !!

Post by brianfry713 »

Input a1a output should be 2
Check input and AC output for thousands of problems on uDebug!
Xenon Kfr
New poster
Posts: 13
Joined: Tue Nov 18, 2014 1:14 am

Re: 494 - Kindergarten Counting Game

Post 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;
        }
    }
}
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 494 - Kindergarten Counting Game

Post 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/
Check input and AC output for thousands of problems on uDebug!
Xenon Kfr
New poster
Posts: 13
Joined: Tue Nov 18, 2014 1:14 am

Re: 494 - Kindergarten Counting Game

Post by Xenon Kfr »

thanx a lot
Post Reply

Return to “Volume 4 (400-499)”