Here's some examples for working with 2d arrays. Its probably best to pass the key as a parameter to the encode functions. Always best to check for the 0 at the end of the line if there's no end of line '\n' char. Its also its better to #define the array sizes 6 and 128.
Code:
void encodeLine(char *getMessage, int key)
{
int i=0;
while( getMessage[i] != '\n' && getMessage[i] != 0)
{
getMessage[i] = (getMessage[i] - 32 +key)%95 +32;
i++;
}
if(getMessage[i] == '\n') // if last char == '\n'
getMessage[i] = 0;
}
void encodeLine2(char *getMessage, int key) //same as above but using pointers
{
char *s = getMessage;
while( *s != '\n' && *s != 0)
{
*s = (*s - 32 +key)%95 +32;
s++;
}
if(*s == '\n') // if last char == '\n'
*s = 0;
}
void encodeAll2(char getMessage[][128], int lines, int key)
{
int i;
for (i = 0; i < lines; i++)
encodeLine(getMessage[i], key);
}
void encodeAll(char getMessage[][128], int lines, int key)
{
int i,k=0;
for (i = 0; i < lines; i++)
{
while( getMessage[i][k] != '\n' && getMessage[i][k] != 0)
{
getMessage[i][k] = (getMessage[i][k] - 32 +key)%95 +32;
k++;
}
if(getMessage[i][k] == '\n') // if the last char is \n
getMessage[i][k] = 0;
}
}
int main()
{
char getMessage[6][128];
int Count = 0, i, key;
while( Count < 6 && *(fgets( getMessage[Count], 128, stdin)) != '\n' )
Count++;
//Encode one line at a time
for(i=0; i < Count; i++)
encodeLine(getMessage[i], key);
//OR
//Encode all
encodeAll(getMessage, Count, key);
//OR
//Another encode all
encodeAll2(getMessage, Count, key);
return 0;
}
>also, where did you learn to program? You're very good at this, any tips for learning?
I started learning C as a hobby around 18 years ago then later C++, been doing it ever since.
The best advice is to keep writing code. Start developing some small programming projects, such as useful utilities etc... Look at other peoples code and examples.
Thanks...
[edit]
removed the extra ')' in:
getMessage[i] = (getMessage[i] - 32 +key)%95 +32;
[edit]