Page 1 of 4
Posted: Sun Mar 10, 2002 2:03 pm
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]='.';
}
}
Posted: Mon Mar 11, 2002 11:20 am
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.
Posted: Mon Mar 11, 2002 12:36 pm
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");
}
}
Posted: Mon Mar 11, 2002 12:46 pm
by kYsis
I forgot to say I got WA ....
Don't see where it can come from ...
Please help !
Posted: Mon Mar 11, 2002 3:02 pm
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.
Posted: Mon Mar 11, 2002 6:57 pm
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 ...
Posted: Tue Mar 12, 2002 2:04 am
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 '''.
Posted: Tue Mar 12, 2002 2:06 am
by Stefan Pochmann
Damn, they deleted the backslash. Should have been a ' then a backslash and then two more '.
Posted: Tue Mar 19, 2002 2:55 am
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);
}
}
Posted: Tue Mar 19, 2002 10:32 am
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>
Posted: Sun Oct 06, 2002 12:00 pm
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]
Posted: Sun Oct 06, 2002 12:04 pm
by haaaz
My test plan is not good enough?
Code: Select all
input : 1234567890-= WERTYUIOP[]\ SDFGHJKL;' XCVBNM,./
output: `1234567890- QWERTYUIOP[] ASDFGHJKL; ZXCVBNM,.
Posted: Mon Oct 21, 2002 4:36 pm
by haaaz
I still don't know why I got WA
Posted: Mon Oct 21, 2002 11:29 pm
by Yarin
You don't seem to handle the new line character! It should also be echoed.
Complie Error
Posted: Wed Oct 23, 2002 12:14 pm
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.