10082 - WERTYU
Moderator: Board moderators
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]='.';
}
}
// 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]='.';
}
}
-
- A great helper
- Posts: 284
- Joined: Thu Feb 28, 2002 2:00 am
- Location: Germany
- Contact:
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.
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.
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");
}
}
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");
}
}
-
- A great helper
- Posts: 284
- Joined: Thu Feb 28, 2002 2:00 am
- Location: Germany
- Contact:
-
- A great helper
- Posts: 284
- Joined: Thu Feb 28, 2002 2:00 am
- Location: Germany
- Contact:
-
- A great helper
- Posts: 284
- Joined: Thu Feb 28, 2002 2:00 am
- Location: Germany
- Contact:
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);
}
}
-
- A great helper
- Posts: 284
- Joined: Thu Feb 28, 2002 2:00 am
- Location: Germany
- Contact:
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...
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>
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) );
}
<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>
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]
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]
My test plan is not good enough?
Code: Select all
input : 1234567890-= WERTYUIOP[]\ SDFGHJKL;' XCVBNM,./
output: `1234567890- QWERTYUIOP[] ASDFGHJKL; ZXCVBNM,.
Complie Error
It seems that I get complie error.
Is it because I declared something like this:
[cpp]const char check[]= "`1234567890-=QWERTYUIOP[]\ASDFGHJKL;'ZXCVBNM,./ nn";[/cpp]
Thank You.
What do they mean?Here are the compiler error messages:
01187393_24.c: In function `int main()':
01187393_24.c:11: unknown escape sequence `\A'
Is it because I declared something like this:
[cpp]const char check[]= "`1234567890-=QWERTYUIOP[]\ASDFGHJKL;'ZXCVBNM,./ nn";[/cpp]
Thank You.
:: HanWorks ::