user173717
user173717

Reputation: 83

C programming, error loop due to scanf?

I have question about scanf a string. I am completing a practice exercise on encoding a Vigenere cipher.

It is easy to solve it at other language but in C I have trouble and I don't know why it is.

This is my code.

#include<stdio.h>
#include<string.h>

main()
{
    char mes[1000];
    char key[100];
    int n=strlen(mes);
    int cipher[n],i,j=0;

    printf("Enter message \n");
    scanf("%s",mes);

    printf("Enter keyword \n");
    scanf("%s",key);
    printf("len message= %d \n",strlen(mes));

    for(i=0;i<strlen(mes);++i)
    {
        mes[i]=toupper(mes[i]);
        key[j]=toupper(key[j]);
        if(j==strlen(key))
            j=0;
        cipher[i]=((mes[i]-0x41)+(key[j]-0x41)) % 26;
        printf("%c",cipher[i]+0x41);
        ++j;}
    printf("\n");
}

My code work well if mes array input is less than 16 character, but if mes input larger the program loop infinite. I don't know why.

If I change type of cipher array to char type, it seem work better, but the result still have wrong output like this:

Enter message 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
Enter keyword 
eeeeeeeeeeeeeeeeeee
len of message= 45
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE�EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEAEEEEEEE

So can you explain:

1) Why my code do wrong if cipher array is an int type?

2) If cipher is char, the output better but why It have wrong output?

Upvotes: 0

Views: 188

Answers (3)

Mysterious Jack
Mysterious Jack

Reputation: 641

In simple words, You wrote int n=strlen(mes); before giving any input,Now how would you determine the size of input when its still not given,See my comments in below code,

char mes[1000];
char key[100];
int n,i,j=0;

printf("Enter message \n");
scanf("%s",mes);
n=strlen(mes);//Determining Size of Input after giving it.....
int cipher[n];//Add This Here,Now You Create a array after Determining input size.

printf("Enter keyword \n");
scanf("%s",key);
printf("len message= %d \n",strlen(mes),strlen(key));

Upvotes: 1

Ari
Ari

Reputation: 92

You didn't initiate the length of the chiper array correctly.. In your code:

char mes[1000];
char key[100];
int n=strlen(mes);
int cipher[n];

The length of chiper array here is undefined.. Unless you write something like:

int chiper[1000];

BR//Ari

Upvotes: 0

GoldRoger
GoldRoger

Reputation: 1263

Couple of problems:

  1. Allocating memory for cipher doesn't work properly

    int n=strlen(mes); // mes has nothing now..so n=0! 
    int cipher[n],i,j=0;
    
  2. Rolling back j should be done after increment .

Put this piece after you read both inputs..it should work for any length<100

 int cipher[strlen(mes)];
 for(i=0;i<strlen(mes);++i)
 {
     mes[i]=toupper(mes[i]);
     key[j]=toupper(key[j]);
     cipher[i]=((mes[i]-'A')+(key[j]-'A')) % 26; //use 'A' for better readability
     printf("%c",cipher[i]+'A');
     ++j;
     if(j==strlen(key))
         j=0;
 }

Upvotes: 1

Related Questions