Thread: So I want to actually make a columnar transposition algorithm to encrypt the messages

  1. #1
    Registered User
    Join Date
    May 2022
    Posts
    22

    So I want to actually make a columnar transposition algorithm to encrypt the messages

    So basically it asks for a key and string. The length of the key determines how many columns in the matrix there are. However I call the strlen function but it doesn't work. It keeps on giving me the length of the string as 6.


    insert
    Code:
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    #include <math.h>
    
    
    int main(){
        char str[50];
        char key[50];
        
        printf("Please enter string:");
        gets(str);
        
        printf("Please enter key:");
        gets(key);
        
        gridStr(str);
        
        return 0;
    }
    void gridStr(char *str, char *key)
    {    
    
    
        int columns = (strlen(key));
        int i = 0;
        while (str[i]) {
            putchar(str[i++]);
            if (i % columns == 0) putchar('\n');
        }
        if (i % columns != 0) putchar('\n');
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Notice that you call gridStr(str) rather than gridStr(str, key). You should compile at a higher warning level so your compiler will warn you about such mistakes.

    When you compile at a higher warning level, you will likely also get a warning about gets. gets is an inherently insecure function that you should never use. You can look into using fgets instead, keeping in mind that it works a little differently.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > gridStr(str);
    Quick quiz - how many parameters does this function take?

    Also, turn up the warnings on your compiler.
    Code:
    $ gcc -Wall -Wextra foo.c
    foo.c: In function ‘main’:
    foo.c:12:5: warning: implicit declaration of function ‘gets’; did you mean ‘fgets’? [-Wimplicit-function-declaration]
       12 |     gets(str);
          |     ^~~~
          |     fgets
    foo.c:17:5: warning: implicit declaration of function ‘gridStr’ [-Wimplicit-function-declaration]
       17 |     gridStr(str);
          |     ^~~~~~~
    foo.c: At top level:
    foo.c:21:6: warning: conflicting types for ‘gridStr’
       21 | void gridStr(char *str, char *key)
          |      ^~~~~~~
    foo.c:17:5: note: previous implicit declaration of ‘gridStr’ was here
       17 |     gridStr(str);
          |     ^~~~~~~
    /usr/bin/ld: /tmp/ccLOGp8t.o: in function `main':
    foo.c:(.text+0x39): warning: the `gets' function is dangerous and should not be used.
    Never EVER use gets() - Why gets() is bad / Buffer Overflows - Cprogramming.com
    Forget it ever existed.
    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.

  4. #4
    Registered User
    Join Date
    May 2022
    Posts
    22

    It takes the str and key

    I don't want to store output to a file. Can I use scanf instead?

  5. #5
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    fgets doesn't "store output", it reads input. And it doesn't need to be from a file. Just use stdin as the input stream. The sad thing about fgets, however, is that it leaves the newline character at the end of the string, which you therefore must usually remove.

    When compiling, use c99 or higher and use a high warning level. E.g.,

    gcc -std=c99 -Wall -Wextra -Wpedantic -Werror myprogram.c -o myprogram

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
     
    int main() {
        char line[256];
     
        if (fgets(line, sizeof line, stdin) == NULL) {
            fprintf(stderr, "No line read\n");
            exit(EXIT_FAILURE);
        }
     
        int len = strlen(line);
        if (len && line[len - 1] == '\n')
            line[--len] = '\0';
     
        printf("<%s>\n", line);
     
        return 0;
    }
    A little inaccuracy saves tons of explanation. - H.H. Munro

  6. #6
    Registered User
    Join Date
    May 2022
    Posts
    22

    How do I rearrange the columns?

    So basically now the code collects the string, key and generates a matrix with column size based on key size. Also it prints the key in alphabetical order. Question is for columnar transposition say the key is "bac" , 'a' is the first character alphabetically so the second column is put first. How do I implement this?

    insert
    Code:
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    #include <math.h>
    
    
    int main(){
        char key[50];
        char line[256];
     
         printf("Enter your string:");
        if (fgets(line, sizeof line, stdin) == NULL) {
            fprintf(stderr, "No line read\n");
            exit(EXIT_FAILURE);
        }
        
        printf("Enter your key");
        if (fgets(key, sizeof key, stdin) == NULL) {
            fprintf(stderr, "No line read\n");
            exit(EXIT_FAILURE);
        }
     
        int len = strlen(line);
        if (len && line[len - 1] == '\n')
            line[--len] = '\0';
            
         int len1 = strlen(key);
        if (len1 && key[len1 - 1] == '\n')
            key[--len]= '\0';
            
        printf("%s\n", line);
        printf("%s\n", key);
        ascendingOrderString(key);
        gridStr(line,key);
    }
    
    
    void gridStr(char *line, char *key)
    {    
        int columns = strlen(key)-1;
        int i = 0;
        while (line[i]) {
            putchar(line[i++]);
            if (i % columns == 0) putchar('\n');
        }
        if (i % columns != 0) putchar('\n');
    }
    
    
    void ascendingOrderString(char *key) {
        char temp;
        int i, j;
        int n = strlen(key);
        for (i = 0; i < n-1; i++) {
            for (j = i+1; j < n; j++) {
                if (key[i] > key[j]) {
                        temp = key[i];
                        key[i] = key[j];
                        key[j] = temp;
                }
            }
        }
    
    
        printf("The sorted string is : %s \n", key);
    }

  7. #7
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    You keep changing what you want your program to do.
    Now it looks like you may need the 2D array you started with.
    I'm out.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  8. #8
    Registered User
    Join Date
    May 2022
    Posts
    22
    Aw man I'm so sorry I realised till at the end I would need the 2D array

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 07-24-2019, 09:51 AM
  2. Replies: 15
    Last Post: 09-15-2013, 02:15 PM
  3. A noob with a question about a transposition cipher
    By DocFerret in forum C++ Programming
    Replies: 10
    Last Post: 09-25-2007, 09:02 AM
  4. I need help for matrix transposition FORMATING
    By masked_blueberr in forum C Programming
    Replies: 6
    Last Post: 07-19-2005, 04:59 PM
  5. Bit Transposition such as D.E.S Initial Permutation
    By silverwatch in forum C++ Programming
    Replies: 1
    Last Post: 12-02-2003, 06:06 AM

Tags for this Thread