# Letting the user determine size of array?

This is a discussion on Letting the user determine size of array? within the C Programming forums, part of the General Programming Boards category; Hello all, I am working on a program called Caesar Cipher which encrypts messages by increasing each character by a ...

1. ## Letter user determine the size of an array?

Hello all,

I am working on a program called Caesar Cipher which encrypts messages by increasing each character by a value determined by the user (key). It will also roll over if the key rolls over the end of the alphabet.

example) S would translate to U with a key of 2. Z would translate to B with a key of 2.

I have the program almost complete, but I am having one problem. I cannot determine how to let the user determine the size of the array. I assume I need to do this in order to adjust the size of the array according to the message. One of the examples messages to test the project is:

If I change the array size to 21 (rather than 80) it will encrypt properly and I will get the encrypted message as:

Jr dkhdg, pdnh pb gdb

The only limit we have is that messages will not exceed 80 characters (I assume I must initialize the size of the array to 80 because of this, correct?). I am having problems encrypting the text properly if the message is < 80. I have to hit enter or input spaces so the message reaches the desired length of 80 before it will then prompt me for the key. Then it will encrypt the message (but of course with all the spaces). How can I determine the size of the array to be the total characters entered by the user even though the initial size is 80? Is there a different approach to this that I am missing?

Here is my code. I apologize as I haven't formatted it yet so I hope its not too difficult to read.

Code:
```#include <stdio.h>

int main()
{

char n[80];
int shift;
int i;
int k;
int aSize = sizeof(n)/sizeof(n[0]);

for (i = 0; i < aSize; i++)
{
scanf ("%c", &n[i]);
}

scanf("%d",&k);

printf("Encrypted message is:");
for (i=0; i < aSize; i++)
{
if ( ( ( n[i] >= 'A' ) && ( n[i] <= 'Z' ) ) || ( (n[i] >= 'a' ) && ( n[i] <= 'z' ) ) )
{
n[i]+=k;

if ((n[i] >= 'Z') && (n[i] <= 'a'))
{
n[i]=((n[i]-'A')) % 26 + 'A';
printf("%c",n[i]);
}
else if (n[i] >= 'z')
{
n[i]=((n[i]-'a')) % 26 + 'a';
printf("%c",n[i]);
}
else
{
printf("%c",n[i]);
}
}
else
{
printf("%c",n[i]);
}
}

return 0;
}```
Can anyone give me a hint towards getting this to work right for messages less than 80?

2. You need to calculate the length after they input a string, not before.

Quzah.

3. What would I put in place "aSize" in the first for loop? char n must have a size of 80 since the character must be less than 80 characters, correct?

The only way I can get this to work is to know the length of the message before I type it in. That example phrase is 21 characters. If I put i < 21 then it will work properly. Although, of course it will not work if the message is greater or less than 21.

When I move the code like so:
Code:
```   printf("Please enter message:");

for (i = 0; i < aSize; i++)
{
scanf ("%c", &n[i]);
}
aSize = sizeof(n)/sizeof(n[0]);printf("Size is %d",aSize);```
It still says that its 80. How do I get the first for loop to stop even when the length is less than 80? I tried using a

Code:
`while (n[i] != '\n')`
but its not working or I must be doing it wrong.

Just a little confused on this part.

Thanks for the help

4. Look up strlen() in your C Library Documentation...

Make your buffer bigger... maybe 1024 ... null terminate your string correctly, use strlen() to determine how much the user typed. There's no reason to encrypt the entire buffer...

5. Originally Posted by Frankie15
I cannot determine how to let the user determine the size of the array.

The only limit we have is that messages will not exceed 80 characters (I assume I must initialize the size of the array to 80 because of this, correct?).
Can anyone give me a hint towards getting this to work right for messages less than 80?
You can set the size two different ways, ask the user for set the size (i.e "enter array size:") the issue I see in having the user input that is it may not be "user friendly" and you'll have to think, "does the user know what an array is?" or you can have it set based on the number of characters the user inputs (i.e character counting). You can take the value you get from the question or the number of characters inputed and use it for setting the size of the array, and even comparing it to make sure it is not over 80.

Code:
```//user input
//calculate the number of characters or take the value the user gives and set to something like numberCharacters

Array[numberCharacters+1] //+1 iff '\0' doesn't count as a character

if (numberCharacters >= 80) fprintf(stderr, "BAD YOU MESSED UP!\n");
else //program works magic```

6. I am still confused on how to do this. We haven't learned strlen yet so I don't believe we can use it.

The program has to run exactly like I have listed (Enter message...then enter shift...then print the encrypted message). I cannot add anything (like asking the user to enter the array size which would make this program much much simpler).

I just don't understand how I can make this program work with strings of any length < 80. When I use the for loop to determine the max as 80 it will not stop the loop (obviously) for anything less than 80 characters. Is it possible to get the loop to stop before 80 characters have been inputted (with a new line character?). I have tried messing around with getting it to stop with a /n character, but I haven't had any luck.

7. This is what I suggest:
• Read in input as a string,.e.g., by using fgets.
• Use an array of 81 characters. The +1 is to account for the null character.
• To loop over the characters of the string, start with the first character and stop when you reach the null character.

8. Originally Posted by Frankie15
We haven't learned strlen yet so I don't believe we can use it.
Any teacher worth a pile of horsepucky in a paddoc would reward your initiative for looking ahead and finding good solutions.

Only a fool would punish initiative in a student.

9. Originally Posted by CommonTater
Any teacher worth a pile of horsepucky in a paddoc would reward your initiative for looking ahead and finding good solutions.

Only a fool would punish initiative in a student.
While I agree, I also see the point of making them learn how to find the end of the string without strlen. If they don't understand looping through a string to find a specific value, future array usage is going to be pretty bleak.

Quzah.

10. @Quzah ... point taken.