Thread: Returning an array in C

  1. #1
    Registered User
    Join Date
    Jun 2016
    Posts
    2

    Smile Returning an array in C

    I know we can't return an array in C as far as they are second class citzen,
    Yet I tried to cope with the problem by returning each by each the characters it contains.
    I'm coding a Vigener encyptioner that returns a code which each letters are differently shifted according to a key word.
    However the university checking system seems to know I am doing intellectual piracy as far as it returns


    Code:
    :( encrypts "world, say hello!" as "xoqmd, rby gflkp!" using "baz" as keyword
       \ expected output, but not "x"
    It is compiled taking the key from the terminal:
    Code:
    ./vigenere bacon
    and is checked through tis command:
    Code:
    make vigenere
    clang -ggdb3 -O0 -std=c11 -Wall -Werror -Wshadow    vigenere.c  -lcs50 -lm -o vigenere
    This is my attempt to code it:

    Code:
    #include <cs50.h>
    #include <stdio.h>
    #include <ctype.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    int main(int argc, string argv[])
    {
    
        char* key= argv[1];
        /*int ASCIIvalue[strlen(key)];
    
        for (int u = 0, q = strlen(key); u < q; u++)
        {
            ASCIIvalue[u]=key[u];
        }*/
    
        printf("What is the text to be encrypted?\n");
        string s = GetString();
        int encryptedASCII[strlen(s)];
    
        for (int i = 0, o = strlen(s); i < o; i++)
        {
            if(i==o)
            {
                break;
            }
    
            char ckey = key[i%strlen(key)];
            int Ikey;
            if(isupper(key[i%strlen(key)]))
            {
                Ikey= ckey -'A';
            }
            else if(islower(key[i%strlen(key)]))
            {
                Ikey = ckey -'a';
            }
    
            if (isspace(s[i]))
            {
                            encryptedASCII[i]=s[i];
            }
    
            else if (isalpha(s[i]))
            {
    
                if(isupper(s[i]))
                {
                //char ckey = key[i%strlen(key)];
                encryptedASCII[i] = ((s[i] - 'A' + Ikey) % 26) + 'A';
    
                }
    
                if(islower(s[i]))
                {
                //char ckey = key[i%strlen(key)];
                encryptedASCII[i] = ((s[i] - 'a' + Ikey) % 26) + 'a';
                }
            }
    
            else
            {
                encryptedASCII[i]=s[i];
                //j--;
            }
    
        }
    
        //here we are displaying the encrypted text 
        for (int i = 0, n = strlen(s); i < n; i++)
        {
            printf("%c", encryptedASCII[i]);
        }
        printf("\n");
    
        return 0;
    Last edited by AntoineCompagni; 06-03-2016 at 02:35 PM. Reason: show the arguments to compile the code

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    For the strings given, b is paired with w, so the result should be x. (Match up row B with column W here.)

    Using substitution with the code, we can check if it's correct.

    ckey = 98
    Ikey = ckey - 97 = 1

    Let's break up encryptedASCII[i] = ((s[i] - 'a'+ Ikey) % 26) + 'a';

    encrypted[i] = (119 - 97 + Ikey) = 23
    encrypted[i] = (23 % 26) = 23
    encrypted[i] = 23 + 97 = 120

    120 in ASCII is x.

    I'm not sure why you think there is a problem.
    Last edited by whiteflags; 06-03-2016 at 06:19 PM.

  3. #3
    Registered User
    Join Date
    Jun 2016
    Posts
    2
    Okay, you were right, there wasn't any problem there, still there is one I don't understand that the checking program revealed:

    Code:
    colocantoine@ide50:~/workspace/pset2 $ check50 2015.fall.pset2.vigenere vigenere.c
    :) vigenere.c exists
    :) vigenere.c compiles
    :) encrypts "a" as "a" using "a" as keyword
    :( encrypts "world, say hello!" as "xoqmd, rby gflkp!" using "baz" as keyword
       \ expected output, but not "xoqmd, szz gflkp!\n"
    :) encrypts "BaRFoo" as "CaQGon" using "BaZ" as keyword
    :) encrypts "BARFOO" as "CAQGON" using "BAZ" as keyword
    :( handles lack of argv[1]
       \ expected output, not a prompt for input
    :( handles argc > 2
       \ expected output, not a prompt for input
    :( rejects "Hax0r2" as keyword
       \ expected output, not a prompt for input
    The first error I want to deal with is:

    Code:
    :( encrypts "world, say hello!" as "xoqmd, rby gflkp!" using "baz" as keyword
       \ expected output, but not "xoqmd, szz gflkp!\n"
    I don't know how to add "\n" to the encrypter without disturbing the already acquired steps above...

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Adding \n is not the solution. It's saying that your output is wrong. The first place it goes wrong is "szz" in your output, where it should be "rby".

    The problem is how your key is handled. Ideally you want it to do this.
    Code:
    text: world, say hello!
    key : bazba, zba zbazb!
    If there is a problem with this part, then the code will select the wrong part of the key, which appears to have happened in your results.

    Look below, this is what your code is currently doing:

    i = 7
    s[7] = 's'
    ckey = key[i%strlen(key)]
    ckey = key[7%3]
    ckey = key[1] = 'a'

    We already have a problem, because s was supposed to be matched with z above, resulting in r.

    So, you may need a special counter for key, that increments as you handle letters. Look at the difference here:

    i = 7
    j = 5
    s[7] = 's'
    ckey = key[j%strlen(key)]
    ckey = key[5%3]
    ckey = key[2] = 'z'

    The correct matchup is made.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 05-09-2012, 06:41 AM
  2. Replies: 4
    Last Post: 10-02-2010, 06:33 AM
  3. Help with returning an array
    By HIT_Braga in forum C Programming
    Replies: 16
    Last Post: 04-09-2010, 03:24 AM
  4. Returning 2D Array
    By Stlcardinal50 in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2009, 02:56 AM
  5. Replies: 6
    Last Post: 11-09-2006, 03:28 AM

Tags for this Thread