492 - Pig-Latin

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

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

Post by Sajid »

Hello turuthok,

i failed to describe it,,

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

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

is it clear now???
Sajid Online: www.sajidonline.com
turuthok
Experienced poster
Posts: 193
Joined: Thu Sep 19, 2002 6:39 am
Location: Indonesia
Contact:

Post by turuthok »

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).
Sajid
Learning poster
Posts: 94
Joined: Sat Oct 05, 2002 5:34 pm
Location: CS - AIUB, Dhaka, Bangladesh.
Contact:

Post by Sajid »

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
Hunter
New poster
Posts: 9
Joined: Wed Feb 12, 2003 10:50 am
Location: Alaska

Post by Hunter »

It's my best, baby! Yet not AC... This time a <0.353 sec> WA.
More trick(s) missed?
[cpp]#include <stdio.h>
#include <string.h>

char str[1000000],word[1000000];
long len,i,j,k;

void main ()
{
while (gets(str))
{
len=strlen(str); j=-1;
for (i=0;i<=len;i++)
{
if ((str>=65 && str<=90) || (str>=97 && str<=122)) {j++; word[j]=str;}
else
{
if ((str[i-1]>=65 && str[i-1]<=90) || (str[i-1]>=97 && str[i-1]<=122))
{
if (word[0]=='a' || word[0]=='e' || word[0]=='i' || word[0]=='o' || word[0]=='u' || word[0]=='A' || word[0]=='E' || word[0]=='I' || word[0]=='O' || word[0]=='U')
{
printf ("%say",word);
}
else
{
for (k=1;k<=j;k++)
{
printf ("%c",word[k]);
}
printf ("%cay",word[0]);
}
for (k=0;k<=j;k++)
{
word[k]='\0';
}
j=-1;
}
if (str!='\0') printf ("%c",str);
}
}
printf ("\n");
}
}[/cpp]
Sajid
Learning poster
Posts: 94
Joined: Sat Oct 05, 2002 5:34 pm
Location: CS - AIUB, Dhaka, Bangladesh.
Contact:

Post by Sajid »

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++) printf("%c",a);
printf("\n");
}[/c]

Using scanf()...

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

Get it now??? :wink:
Sajid Online: www.sajidonline.com
Nick
Learning poster
Posts: 53
Joined: Sun Jan 12, 2003 4:49 am

about inputing a line

Post by Nick »

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
Sajid
Learning poster
Posts: 94
Joined: Sat Oct 05, 2002 5:34 pm
Location: CS - AIUB, Dhaka, Bangladesh.
Contact:

sorry

Post by Sajid »

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.
Contact:

Post by Sajid »

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
turuthok
Experienced poster
Posts: 193
Joined: Thu Sep 19, 2002 6:39 am
Location: Indonesia
Contact:

Post by turuthok »

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
Contact:

Post by turuthok »

Hunter, ... try this input:

Code: Select all

abc123def
-turuthok-
The fear of the LORD is the beginning of knowledge (Proverbs 1:7).
adikhosla
New poster
Posts: 3
Joined: Fri Feb 14, 2003 3:12 pm

Post by adikhosla »

I have tested and tried the code. It does work. Maybe its the compiler that you are using to compile it. I am using Microsoft Visual C++ .NET. When I submit this code to the online judge I get a WA, when I change the array length to 1000000 and not a TLE or RTE, which might suggest what you are saying. I am sorry for writing the wrong array length. Just for clarification, arent you supposed to print out the digits or any other characters before and after the words?
(non-words should be output exactly as they appear in the input):
Thanks for replying,
Aditya
Hunter
New poster
Posts: 9
Joined: Wed Feb 12, 2003 10:50 am
Location: Alaska

Post by Hunter »

I don't understand..! Shouldn't it result:

Code: Select all

abcay123efday
razibcse
New poster
Posts: 50
Joined: Mon Jul 22, 2002 3:17 am
Location: SUST,BANGLADESH
Contact:

Post by razibcse »

To Sajid:
Of course, it's ANSI standard...
in many problems, you will get RTE if you use smaller array

Razib
Hunter
New poster
Posts: 9
Joined: Wed Feb 12, 2003 10:50 am
Location: Alaska

Post by Hunter »

I don't like this!!! This prob. keeps me WA for weeks...!!
I've handled the "abc123def" input to result "abc123defay". Is it correct?!
Few I/O cases follow up:

Code: Select all

a1
a.a.a.
a.a...
a1.
.a1?
1a...a
p..p
What they should result?
Do the "consecutive sequence of letters (upper and/or lower case)" takes alphaNUMERIC characters?

Hunting 4 answers,
Hunter
Sajid
Learning poster
Posts: 94
Joined: Sat Oct 05, 2002 5:34 pm
Location: CS - AIUB, Dhaka, Bangladesh.
Contact:

thanx all

Post by Sajid »

at last i got AC ... :D

thanx to all.. specially Turuthok.

c ya ... :wink:
Sajid Online: www.sajidonline.com
Post Reply

Return to “Volume 4 (400-499)”