Thread: Print an array in blocks of five?

  1. #1
    Registered User
    Join Date
    Mar 2015
    Posts
    7

    Print an array in blocks of five?

    I wrote this code for a Caesar cipher but am stuck at the end.

    I have three function:
    processFile() opens a file (congress.txt) and stores it in an array.
    cipher() shifts all the letters up one letter.
    outputCode() is supposed to print the new text in blocks of five, ten blocks per line.

    That is where I am stuck. I am able to go to a new line every 50 characters but can't figure out how to get the characters in blocks of five.

    My ouput:


    DPOHSFTTTIBMMNBLFOPMBXSFTQFDUJOHBOFTUBCMJTINFOUPGS
    FMJHJPOPSQSPIJCJUJOHUIFGSFFFYFSDJTFUIFSFPGPSBCSJEH
    JOHUIFGSFFEPNPGTQFFDIPSPGUIFQSFTTPSUIFSJHIUPGUIFQF
    PQMFQFBDFBCM@UPBTTFNCMFBOEUPQFUJUJPOUIFHPWFSONFOUG
    PSBSFESFTTPGHSJFWBODFT

    Desired output:


    DPOHS FTTTI BMMNB LFOPM BXSFT QFDUJ OHBOF TUBCM JTINF OUPGS
    FMJHJ POPSQ SPIJC JUJOH UIFGS FFFYF SDJTF UIFSF PGPSB CSJEH
    JOHUI FGSFF EPNPG TQFFD IPSPG UIFQS FTTPS UIFSJ HIUPG UIFQF
    PQMFQ FBDFB CM@UP BTTFN CMFBO EUPQF UJUJP OUIFH PWFSO NFOUG
    PSBSF ESFTT PGHSJ FWBOD FT


    Any suggestions?


    Here is my code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    FILE *csis;
    
    
    //Functions to be used.
    void processFile(int *store);
    void cipher(int *store, int *code);
    void outputCode(int *code);
    
    
    int main()
    {
        csis = fopen("csis.txt", "w");
    
    
        int store[300], code[300], i;
    
    
        //Order of functions to be called.
        processFile(store);
        cipher(store, code);
        outputCode(code);
    
    
        fclose(csis);
        return 0;
    }
    
    
    //Function to open the Congress text
    //file and set it up for cipher.
    void processFile(int *store){
    
    
        int i;
        char a = 0;
        FILE *f = fopen("congress.txt", "r");
    
    
        //Stores lowercase letters as uppercase, etc.
        for (i = 0; !feof(f) && i<299;){
            fscanf(f, "%c", &a);
            if (a <= 'Z' && a >= 'A'){
                store[i] = a;
                i++;
            }
            if (a <= 'z' && a >= 'a'){
                store[i] = a - 32;
                i++;
            }
        }
        store[i]='\0';
    }
    
    
    //Function to execute the shift by 1 letter.
    void cipher(int *store, int *code){
    
    
        int i;
    
    
        for (i = 0; store[i] != 0; ++i){
            if (store[i] <= 'X' && store[i] >= 'A'){    //Tests to see if the letter is between A and X.
                code[i] = (char)(store[i] + 1);         //Shifts letter by one characters.
            }
            if (store[i] >= 'Y' && store[i] <= 'Z'){
                code[i] = (char)(store[i] - 25);        //Shifts Y and Z.
            }
        }
    
    
        for(; i<300; i++) code[i]=0; //Pad with zeros.
    }
    
    
    //Function to output the final encoded message in blocks.
    void outputCode(int *code){
    
    
        int i, a, b;
    
    
        for (a = 0; code[a] != 0; ++a){
    
    
            //New line every 50 characters.
            if (!(a % 50)){
                printf("\n");
                fprintf(csis, "\n");
            }
    
    
            printf("%c", code[a]);
            fprintf(csis, "%c", code[a]);
    
    
        }
    }
    Last edited by antonio.gor; 11-12-2015 at 05:52 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > //New line every 50 characters.
    Well you figured out this much.

    How hard can it be to have a %5 to print spaces as well?

    Also, make your csis variable local to main, and pass it as a parameter when needed.

    But since you've just copied code from somewhere else, I guess you'll never figure it out.
    http://stackoverflow.com/questions/2...-c-programming
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can't seem to print an array, HELP !
    By Demonoid in forum C Programming
    Replies: 13
    Last Post: 03-06-2011, 05:59 PM
  2. Replies: 2
    Last Post: 04-27-2008, 03:39 AM
  3. Identifying 3x3 blocks (2d-array)
    By Shapper in forum C++ Programming
    Replies: 4
    Last Post: 04-29-2007, 01:12 PM
  4. i want to print a array
    By timhxf in forum C Programming
    Replies: 2
    Last Post: 01-07-2007, 04:25 AM
  5. get nxn size blocks from 2d array?
    By acfror in forum C Programming
    Replies: 4
    Last Post: 10-28-2005, 03:18 AM