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
Post
by arafat13 » Mon Jul 22, 2013 9:42 pm
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
Post
by brianfry713 » Tue Jul 23, 2013 12:49 am
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
Post
by arafat13 » Tue Jul 23, 2013 9:05 am
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
Post
by raihan_sust5 » Fri Jul 26, 2013 12:20 pm
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
Post
by brianfry713 » Sat Jul 27, 2013 3:31 am
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
Post
by muktadir.rhmn » Fri May 09, 2014 5:05 pm
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
Post
by brianfry713 » Fri May 09, 2014 8:02 pm
Check input and AC output for thousands of problems on
uDebug !
pnkj_agrl
New poster
Posts: 2 Joined: Thu May 08, 2014 9:36 pm
Post
by pnkj_agrl » Sat May 10, 2014 10:14 pm
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
Post
by brianfry713 » Mon May 12, 2014 10:20 pm
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
Post
by saif14 » Wed Jun 04, 2014 2:12 pm
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
Post
by brianfry713 » Wed Jun 11, 2014 10:26 pm
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
Post
by Xenon Kfr » Wed Nov 19, 2014 8:48 pm
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;
}
}
}