492 - Pig Latin

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

Moderator: Board moderators

492 - Pig Latin

Postby Sajid » Thu Feb 20, 2003 4:15 pm

I got WA with my code.. i dont know why.....

the output is ok , i think. But, i think.. the only problem with to scan a number frm the user or.. for the newline at the end of each output.

In the previous post of this board I found ,, to use scanf("%s",input) .... istead of gets(input),, but why??????????

if i use gets then, what is the problem? and if i use scanf then how can make a new line at the end of each output..

I hope, u'll try to help me.
Sajid
Learning poster
 
Posts: 94
Joined: Sat Oct 05, 2002 5:34 pm
Location: CS - AIUB, Dhaka, Bangladesh.

Postby turuthok » Fri Feb 21, 2003 2:58 am

I don't think you can use scanf("%s") here since I'm sure "%s" format ignores white-space ...

My slow but AC-ed Java program reads the input one character at a time and maintains a simple state throughout the runtime.

-turuthok-
The fear of the LORD is the beginning of knowledge (Proverbs 1:7).
turuthok
Experienced poster
 
Posts: 193
Joined: Thu Sep 19, 2002 6:39 am
Location: Indonesia

About the critical input

Postby Sajid » Fri Feb 21, 2003 6:01 am

Thanx so much for ur help. now I m using gets() function... but still wrong answer...

can u tell me some critical input and output?????


c ya ....
Sajid
Learning poster
 
Posts: 94
Joined: Sat Oct 05, 2002 5:34 pm
Location: CS - AIUB, Dhaka, Bangladesh.

Postby Sajid » Sat Feb 22, 2003 11:22 pm

To turuthok,

As for Inforation, u can also use scanf() function as like the following
[c]
while(scanf("%s%c",a,&ch)==2)
.
.
.
.
printf("%s%c",a,ch);[/c]

get it??


Can anyone tell me the critical inputs for this problem???
Sajid Online: www.sajidonline.com
Sajid
Learning poster
 
Posts: 94
Joined: Sat Oct 05, 2002 5:34 pm
Location: CS - AIUB, Dhaka, Bangladesh.

Postby turuthok » Sat Feb 22, 2003 11:31 pm

Hello Sajid, ... I see ... I never used scanf("%s%c", ...) function like you described ... Will it work if I have an input like this: "abcde fghij" ?

I'm sure it's a little bit confusing coding it using while(scanf("%s%c", ...) == 2) ... Or I'm just too skeptical about that ...

I didn't try your method though ... I should.

-turuthok-
The fear of the LORD is the beginning of knowledge (Proverbs 1:7).
turuthok
Experienced poster
 
Posts: 193
Joined: Thu Sep 19, 2002 6:39 am
Location: Indonesia

Postby turuthok » Sat Feb 22, 2003 11:33 pm

Oops, I didn't realize multiple blanks will be truncated ... sorry.
Sajid, the input I mentioned above should be:
Code: Select all
abcde    fghij


-turuthok-
[/code]
The fear of the LORD is the beginning of knowledge (Proverbs 1:7).
turuthok
Experienced poster
 
Posts: 193
Joined: Thu Sep 19, 2002 6:39 am
Location: Indonesia

Postby Sajid » Sat Feb 22, 2003 11:40 pm

Hello turuthok,

i failed to describe it,,

i meant, instead of this code:
[c]while(gets(a))
{
...
...
.....printf("%c",a[i]);
}
printf("\n");
[/c]

we can also use this...[c]while(scanf("%s%c",a,&ch)==2)
{
...
...
...
.....prinft("%c",a[i]);
}
printf("%c",&ch);[/c]

is it clear now???
Sajid Online: www.sajidonline.com
Sajid
Learning poster
 
Posts: 94
Joined: Sat Oct 05, 2002 5:34 pm
Location: CS - AIUB, Dhaka, Bangladesh.

Postby turuthok » Sun Feb 23, 2003 1:10 am

Sajid, forgive me ... but I still cannot see how you handle multiple spaces between words using your method of "%s%c" stuff ...

If it works, ... I'm happy for you :)

Regards,

-turuthok-
The fear of the LORD is the beginning of knowledge (Proverbs 1:7).
turuthok
Experienced poster
 
Posts: 193
Joined: Thu Sep 19, 2002 6:39 am
Location: Indonesia

Postby Sajid » Sun Feb 23, 2003 7:03 am

turuthok, look ...

[c]while(scanf("%s%c",a,&ch)==2)
{

}
[/c]

here, the single word is stroed in a[] and the next non-alphabet(digit, space, newline etc.) is stored in ch.
and u can do the loop word by word and after completing each loop, jst print the ch variable.. which stored a non-alphabetic chanracter..
and that's it... It works in my program.
It's cute technic, isn't it? :wink:

But my program still WA.......................
Sajid Online: www.sajidonline.com
Sajid
Learning poster
 
Posts: 94
Joined: Sat Oct 05, 2002 5:34 pm
Location: CS - AIUB, Dhaka, Bangladesh.

Postby Sajid » Sun Feb 23, 2003 8:43 pm

I think, u still cant get..

ok, lets compare the following two almost the same programs.

Using gets()...

[c]char a[100];
int i;
while(gets(a))
{
for(i=0;a[i];i++) printf("%c",a[i]);
printf("\n");
}[/c]

Using scanf()...

[c]char a[100],ch;
int i;
while(scanf("%s%c",a,&ch)==2)
{
for(i=0;a[i];i++) printf("%c",a[i]);
printf("%c",ch);
}[/c]

Get it now??? :wink:
Sajid Online: www.sajidonline.com
Sajid
Learning poster
 
Posts: 94
Joined: Sat Oct 05, 2002 5:34 pm
Location: CS - AIUB, Dhaka, Bangladesh.

about inputing a line

Postby Nick » Mon Feb 24, 2003 4:54 am

To Sajid,
your code
Code: Select all
while(scanf("%s%c",a,&ch)==2)
{
 
}


it has a bug.....since scanf ignores white spaces....your module wont stop until it reached end of file.
Meanwhile, gets function stops when it encounters '\n' or carriage return '\r'. So....your code cannot get one line at a time. Ex :

Input :
What if the input is more than one line?
Like this sample


Using your module.... string a will contain "sample".
But using gets function, a contain "What if the input is more than one line?"
Then....the program starts to process the string to get the proper output
before gets another line.

Hope i'm right,
Nick
Nick
Learning poster
 
Posts: 53
Joined: Sun Jan 12, 2003 4:49 am

sorry

Postby Sajid » Mon Feb 24, 2003 8:59 am

To Nick,
Yes.. i m so sorry that my code has some bugs which i overlooked...

thanx nick.. :wink:
Sajid Online: www.sajidonline.com
Sajid
Learning poster
 
Posts: 94
Joined: Sat Oct 05, 2002 5:34 pm
Location: CS - AIUB, Dhaka, Bangladesh.

Postby Sajid » Mon Feb 24, 2003 9:02 am

turuthok wrote:Sajid, the input I mentioned above should be:
Code: Select all
abcde    fghij


-turuthok-


Turuthok,sorry, my code will not work with the above input..
sorry for that....

--- Sajid
Sajid Online: www.sajidonline.com
Sajid
Learning poster
 
Posts: 94
Joined: Sat Oct 05, 2002 5:34 pm
Location: CS - AIUB, Dhaka, Bangladesh.

Postby turuthok » Mon Feb 24, 2003 11:07 am

Sajid, don't worry about it ... :)

I hope you'll get this problem AC-ed soon ...

-turuthok-
The fear of the LORD is the beginning of knowledge (Proverbs 1:7).
turuthok
Experienced poster
 
Posts: 193
Joined: Thu Sep 19, 2002 6:39 am
Location: Indonesia

thanx all

Postby Sajid » Wed Mar 05, 2003 9:54 pm

at last i got AC ... :D

thanx to all.. specially Turuthok.

c ya ... :wink:
Sajid Online: www.sajidonline.com
Sajid
Learning poster
 
Posts: 94
Joined: Sat Oct 05, 2002 5:34 pm
Location: CS - AIUB, Dhaka, Bangladesh.

Next

Return to Volume IV

Who is online

Users browsing this forum: No registered users and 0 guests