10082 - WERTYU

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

Moderator: Board moderators

kYsis
New poster
Posts: 8
Joined: Sun Mar 10, 2002 2:00 am

Post by kYsis »

I don't understand the problem in my source : I don't have any warning when I compile it, but it crashes ...


// Be careful, my whole source is down there ...
















































#include<stdio.h>

#define TAILLE 1000

char convert(char phrase[TAILLE]);

int main()
{
int tmple,tmpnb,i;
char phrase[TAILLE];

while( (scanf("%s",phrase))!='n')
{
scanf("%s",phrase);
printf("%sn",convert(phrase));
}

return 0;
}

char convert(char phrase[TAILLE])
{
int i;
for( i=0;i<=TAILLE;i++)
{
if( phrase=='1') phrase='`';
if( phrase=='2') phrase='1';
if( phrase=='3') phrase='2';
if( phrase=='4') phrase='3';
if( phrase=='5') phrase='4';
if( phrase[i]=='6') phrase[i]='5';
if( phrase[i]=='7') phrase[i]='6';
if( phrase[i]=='8') phrase[i]='7';
if( phrase[i]=='9') phrase[i]='8';
if( phrase[i]=='0') phrase[i]='9';
if( phrase[i]=='-') phrase[i]='0';
if( phrase[i]=='=') phrase[i]='-';
if( phrase[i]=='W') phrase[i]='Q';
if( phrase[i]=='E') phrase[i]='W';
if( phrase[i]=='R') phrase[i]='E';
if( phrase[i]=='T') phrase[i]='R';
if( phrase[i]=='Y') phrase[i]='T';
if( phrase[i]=='U') phrase[i]='Y';
if( phrase[i]=='I') phrase[i]='U';
if( phrase[i]=='O') phrase[i]='I';
if( phrase[i]=='P') phrase[i]='O';
if( phrase[i]=='[') phrase[i]='P';
if( phrase[i]==']') phrase[i]='[';
if( phrase[i]=='a') phrase[i]=']';
if( phrase[i]=='S') phrase[i]='A';
if( phrase[i]=='D') phrase[i]='S';
if( phrase[i]=='F') phrase[i]='D';
if( phrase[i]=='G') phrase[i]='F';
if( phrase[i]=='H') phrase[i]='G';
if( phrase[i]=='J') phrase[i]='H';
if( phrase[i]=='K') phrase[i]='J';
if( phrase[i]=='L') phrase[i]='K';
if( phrase[i]==';') phrase[i]='L';
if( phrase[i]=='&') phrase[i]=';';
if( phrase[i]=='X') phrase[i]='Z';
if( phrase[i]=='C') phrase[i]='X';
if( phrase[i]=='V') phrase[i]='C';
if( phrase[i]=='B') phrase[i]='V';
if( phrase[i]=='N') phrase[i]='B';
if( phrase[i]=='M') phrase[i]='N';
if( phrase[i]==',') phrase[i]='M';
if( phrase[i]=='.') phrase[i]=',';
if( phrase[i]=='/') phrase[i]='.';
}
}

Stefan Pochmann
A great helper
Posts: 284
Joined: Thu Feb 28, 2002 2:00 am
Location: Germany
Contact:

Post by Stefan Pochmann »

Do you think when a programs compiles without warnings, then it can't crash? In that case: Welcome to reality, Neo.

One problem is your use of scanf. It returns the number of successfully read tokens, so in your case usually 1. Comparing this with 'n' doesn't make sense.

Furthermore, scanf("%s") doesn't read the whole line, so you might have trouble when there are space characters. And why do you have two consecutive scanf's there?

Last, but not least, your phrase[] has 1000 elements, that is, phrase[0..999]. But you also access phrase[1000], which is one of the safest ways to go to hell.

kYsis
New poster
Posts: 8
Joined: Sun Mar 10, 2002 2:00 am

Post by kYsis »

I made a whole new one :
Works perfectly ...




#include<stdio.h>

char convert(char ch)
{
if( ch=='1') return '`';
if( ch=='2') return '1';
if( ch=='3') return '2';
if( ch=='4') return '3';
if( ch=='5') return '4';
if( ch=='6') return '5';
if( ch=='7') return '6';
if( ch=='8') return '7';
if( ch=='9') return '8';
if( ch=='0') return '9';
if( ch=='-') return '0';
if( ch=='=') return '-';
if( ch=='W') return 'Q';
if( ch=='E') return 'W';
if( ch=='R') return 'E';
if( ch=='T') return 'R';
if( ch=='Y') return 'T';
if( ch=='U') return 'Y';
if( ch=='I') return 'U';
if( ch=='O') return 'I';
if( ch=='P') return 'O';
if( ch=='[') return 'P';
if( ch==']') return '[';
if( ch=='?') return ']';
if( ch=='S') return 'A';
if( ch=='D') return 'S';
if( ch=='F') return 'D';
if( ch=='G') return 'F';
if( ch=='H') return 'G';
if( ch=='J') return 'H';
if( ch=='K') return 'J';
if( ch=='L') return 'K';
if( ch==';') return 'L';
if( ch==92) return ';'; /* ASCII because '' makes an error */
if( ch=='X') return 'Z';
if( ch=='C') return 'X';
if( ch=='V') return 'C';
if( ch=='B') return 'V';
if( ch=='N') return 'B';
if( ch=='M') return 'N';
if( ch==',') return 'M';
if( ch=='.') return ',';
if( ch=='/') return '.';
}




int main()
{
char line[256];
char* p;

while( gets(line))
{
p=line;
while(*p)
if( *p == '')
printf("n");
else
printf("%c",convert(*p++));
printf("n");
}
}

kYsis
New poster
Posts: 8
Joined: Sun Mar 10, 2002 2:00 am

Post by kYsis »

I forgot to say I got WA ....
Don't see where it can come from ...
Please help !

Stefan Pochmann
A great helper
Posts: 284
Joined: Thu Feb 28, 2002 2:00 am
Location: Germany
Contact:

Post by Stefan Pochmann »

First of all, make your array larger, like 10000. Then, you need to replace ' ' (space character) with itself. And do you really use the keyboard shown in the problem? You know, you might have a different one at home.

kYsis
New poster
Posts: 8
Joined: Sun Mar 10, 2002 2:00 am

Post by kYsis »

I do use the keyboard shown on the problem, because I'm French and I have AZERTY keyboards at home ...
For ' ' I think it is ok, I tested the algorithm in another program with just a scanf for a single line, and I works perfectly good ...

Stefan Pochmann
A great helper
Posts: 284
Joined: Thu Feb 28, 2002 2:00 am
Location: Germany
Contact:

Post by Stefan Pochmann »

But if you do, then why do you have a '?' char? and where is the backslash? Btw, you can escape char(92) like '''.

Stefan Pochmann
A great helper
Posts: 284
Joined: Thu Feb 28, 2002 2:00 am
Location: Germany
Contact:

Post by Stefan Pochmann »

Damn, they deleted the backslash. Should have been a ' then a backslash and then two more '.

C8H10N4O2
Experienced poster
Posts: 137
Joined: Wed Feb 27, 2002 2:00 am
Location: Pasadena, CA

Post by C8H10N4O2 »

Forgive me, but how long did that coding take? All that copy and paste seems a bit tedious. Here is how I think it should be done. How did you do it Stephen?

Code: Select all

#include <stdio.h>

void main()
{
	char TA[]="`1234567890-=QWERTYUIOP[]ASDFGHJKL;'ZXCVBNM,./",c;
	int i;

	while(scanf("%c",&c)!=EOF)
	{
		for(i=0;i<47;i++)
		{
			if(c==TA[i])
			{
				c=TA[i-1];
				break;
			}
		}
		printf("%c",c);
	}
}

Stefan Pochmann
A great helper
Posts: 284
Joined: Thu Feb 28, 2002 2:00 am
Location: Germany
Contact:

Post by Stefan Pochmann »

Usually I would never post a solution here, and I discourage from doing so. But since you posted yours already and mine is almost the same...

I hope the backslashes make it this time. I think they delete them when you don't escape them. Yes, they do. And when you edit a message, you have to escape them again...

Code: Select all

int main () {
  char c, convert[] = "`1234567890-=QWERTYUIOP[]\ASDFGHJKL;'ZXCVBNM,./  nn";
  while( (c = getchar()) != EOF )
    putchar( *(strrchr( convert, c ) - 1) );  
}
Of course both our solutions are damn slow, building a lookup-table would be appropriate. Unfortunately, this problem can be solved in zero time anyway...

<font size=-1>[ This Message was edited by: Stefan Pochmann on 2002-03-19 09:35 ]</font>

<font size=-1>[ This Message was edited by: Stefan Pochmann on 2002-03-19 09:39 ]</font>

<font size=-1>[ This Message was edited by: Stefan Pochmann on 2002-03-19 09:42 ]</font>

haaaz
New poster
Posts: 29
Joined: Sun Sep 08, 2002 8:02 am

Post by haaaz »

But I still do not know why I got WA.
I have checked every character already.

[cpp]#include <iostream.h>

int main() {

char code[] = { 'A','V','X','S','W','D','F','G','U','H','J','K','N','B',
'I','O','Q','E','A','R','Y','C','Q','Z','T','Z'
};

char ch;
while (cin.get(ch)) {
if ('A' <= ch && ch <= 'Z')
cout << code[int(ch)-65];
else if ('2' <= ch && ch <= '9')
cout << int(ch)-49;
else
switch (ch) {
case ' ' : cout << ' ';
break;
case '[' : cout << 'P';
break;
case ']' : cout << '[';
break;
case ';' : cout << 'L';
break;
case '\'' : cout << ';';
break;
case ',': cout << 'M';
break;
case '.': cout << ',';
break;
case '/': cout << '.';
break;
case '\\': cout << ']';
break;
case '1': cout << '`';
break;
case '0': cout << '9';
break;
case '-': cout << '0';
break;
case '=': cout << '-';
}
}

return 0;
}[/cpp]

haaaz
New poster
Posts: 29
Joined: Sun Sep 08, 2002 8:02 am

Post by haaaz »

My test plan is not good enough?

Code: Select all

input : 1234567890-= WERTYUIOP[]\ SDFGHJKL;' XCVBNM,./
output: `1234567890- QWERTYUIOP[] ASDFGHJKL; ZXCVBNM,.

haaaz
New poster
Posts: 29
Joined: Sun Sep 08, 2002 8:02 am

Post by haaaz »

I still don't know why I got WA

Yarin
Problemsetter
Posts: 112
Joined: Tue Sep 10, 2002 5:06 am
Location: Ume
Contact:

Post by Yarin »

You don't seem to handle the new line character! It should also be echoed.

Ming Han
Learning poster
Posts: 77
Joined: Thu Jun 06, 2002 7:10 pm
Location: Singapore
Contact:

Complie Error

Post by Ming Han »

It seems that I get complie error.
Here are the compiler error messages:

01187393_24.c: In function `int main()':
01187393_24.c:11: unknown escape sequence `\A'
What do they mean?

Is it because I declared something like this:
[cpp]const char check[]= "`1234567890-=QWERTYUIOP[]\ASDFGHJKL;'ZXCVBNM,./ nn";[/cpp]

Thank You.
:: HanWorks ::

Post Reply

Return to “Volume 100 (10000-10099)”