-
6x6 array problem
Hi there! I want to do an 6x6 array which contains the english abc + numbers 0-9 (sum: 36 chars). This will be a polybius cipher. I read words from a txt file (for example apple11) and write its number to a txt file (apple11 = 11 34 34 26 15 54 54). It work, but got wrong results. Thanks
Code:
#include <stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
int main(void){
int i,j,k,x;
int tomb[36];
/*char str[500];
gets(str);*/
FILE* fp = fopen ("be.txt", "r");
char str[500];
char* filename = "be.txt";
fgets(str, 500, fp); // ez lesz a fájlból beolvasás
x=0;
k=11;
for(i=0;i<=36;i++)
{
if(i<=6)
{
if(i%6==0&&i!=0)
{
k+=1;
tomb[x]=k++;
}
else{
tomb[x]=k++;
}
}
if(i>=6)
{
if(i%6==0&&i!=0)
{
tomb[x]=k++;
k+=1;
}
else{
tomb[x]=k++;
}
}
if(i==35)
{
tomb[x]=36;
}
x++;
}
FILE* outfile = fopen("ki.txt", "w");
i=0;
while(str[i]!='\0')
{
if(!((str[i]>=0&&str[i]<=31)||(str[i]>=33&&str[i]<65)||(str[i]>90&&str[i]<97)||(str[i]>122&&str[i]<=127)))
{
if(str[i]>='A'&&str[i]<='Z')
{
fprintf(outfile,"%d\n ",tomb[str[i]-'A']);
}
if(str[i]>='a'&&str[i]<='z')
{
fprintf(outfile,"%d\n ",tomb[str[i]-'a']);
}
}
if(str[i]==' ')
{
fprintf(outfile,"%c\n",str[i]);
}
i++;
}
}
-
My first thought is, indent your code properly.
Code:
#include <stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
int main(void)
{
int i, j, k, x;
int tomb[36];
/*char str[500];
gets(str); */
FILE *fp = fopen("be.txt", "r");
char str[500];
char *filename = "be.txt";
fgets(str, 500, fp); // ez lesz a fájlból beolvasás
x = 0;
k = 11;
for (i = 0; i <= 36; i++)
{
if (i <= 6) {
if (i % 6 == 0 && i != 0) {
k += 1;
tomb[x] = k++;
} else {
tomb[x] = k++;
}
}
if (i >= 6) {
if (i % 6 == 0 && i != 0) {
tomb[x] = k++;
k += 1;
} else {
tomb[x] = k++;
}
}
if (i == 35) {
tomb[x] = 36;
}
x++;
}
FILE *outfile = fopen("ki.txt", "w");
i = 0;
while (str[i] != '\0') {
if (!((str[i] >= 0 && str[i] <= 31) || (str[i] >= 33 && str[i] < 65)
|| (str[i] > 90 && str[i] < 97) || (str[i] > 122 && str[i] <= 127))) {
if (str[i] >= 'A' && str[i] <= 'Z') {
fprintf(outfile, "%d\n ", tomb[str[i] - 'A']);
}
if (str[i] >= 'a' && str[i] <= 'z') {
fprintf(outfile, "%d\n ", tomb[str[i] - 'a']);
}
}
if (str[i] == ' ') {
fprintf(outfile, "%c\n", str[i]);
}
i++;
}
}
> for (i = 0; i <= 36; i++)
Then I think you're running off the end of your array.
> if (!((str[i] >= 0 && str[i] <= 31) || (str[i] >= 33 && str[i] < 65)
> || (str[i] > 90 && str[i] < 97) || (str[i] > 122 && str[i] <= 127)))
Then I'm wondering whether this is just a complicated way of saying isalpha() or complete gibberish.
> if (i <= 6)
> if (i >= 6)
So what happens when i == 6 ?
Perhaps you could print every entry of tomb[x] when you're done filling the array.