Thread: two dimensional arrays help

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    2

    two dimensional arrays help

    Simple Encryption and decryption
    Question is
    The user will enter a word (a maximum of 20 characters or until the user presses Enter) and your program should encrypt or decrypt the word based on the users choice. The idea is to find the correct letter and pring the corresponding one.

    The code I did is

    insert
    Code:
    #include <stdio.h>
    int main()
    {
        char original[27]{'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',' '};
        char encrypted[27]{'H','F','L','R','U','O','P','C','B','A','Z','E','W','D','G','Y','I','M','K','N','X','S','J','V','Q',' ','V'};
        char word[20];
        char processed[20];
        int i,j;
        printf("Please enter the word: \n");
        for(i=0; i<20; i++){
                 scanf("%c", &word[i]);
                 if(word[i] == '\n')
        }
        
        for(i=0; i<20; i++)
        for(j=0; j<27; j++){
            if(word[i] == original[j]);
            
        }
    I couldnt continue

    Thank you so much for your time

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Why couldn't you continue? I remember I had to write this in assembly for an antique processor when I was in university.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    2
    Its a compulsory subject from the university although it won't open for me any course later on since my major doesnt include programming n those stuff.. its hard
    The point where im stuck at is how can i compare the word that ill enter it with the encrypted one?
    I didnt know how to use the if Function
    thank you

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    You compare strings with strcmp. I suggest you Google it and look over some examples.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Two Dimensional Arrays
    By Lock in forum C Programming
    Replies: 7
    Last Post: 10-20-2011, 11:04 PM
  2. 2 dimensional arrays
    By naserian in forum C Programming
    Replies: 5
    Last Post: 03-22-2010, 04:40 PM
  3. Three ONE dimensional arrays
    By victory1 in forum C Programming
    Replies: 5
    Last Post: 11-16-2009, 07:14 AM
  4. Help with 2 dimensional arrays
    By riotact in forum C++ Programming
    Replies: 1
    Last Post: 11-02-2002, 10:19 AM
  5. Two dimensional arrays
    By Jax in forum C Programming
    Replies: 1
    Last Post: 11-07-2001, 12:53 PM