Sorting Strings in C

There are many techniques for encoding and encrypting text. Encoding text means modifying the text’s format so that it can be interpreted by another system; encrypting text means modifying the text so that it can only be interpreted by designated parties.

A transposition cypher is a simple means for encrypting text. In this technique, the text is written as a matrix of characters, then the matrix is transposed and the characters read out.

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <malloc.h>

int main(void){

    char input[100];
    char matrix[10][10];

    int i, spaces, row, column, lineLength, stringLength;

    printf("Please enter a text string to encrypt: \n");
    
    fgets(input, sizeof(input), stdin);

    spaces = 0;
    stringLength = strlen(input);

    for(i = 0; i < stringLength; i++){
        if(input[i]==' '){
            spaces++;
        }
    }

    lineLength = 1 + sqrt(stringLength - spaces);

    //fill matrix with empty spaces
    for(row = 0; row < 10; row++){
        for(column = 0; column < 10; column++){
            matrix[row][column] = ' ';
        }
    }

    row = 0;
    column = 0;
    i = 0;

    while(input[i]!='')
    {
        //leave blank spaces be
        if(input[i]!=' ' && input[i]!='\n'){
            matrix[row][column] = input[i];
            row++;
            //have we reached the end of the line?
            if(row==lineLength){
                row = 0;
                column++;
            }
        }
        i++;
    } //end while

    //print out matrix
    for(row = 0; row < 10; row++){
        for(column = 0; column < 10; column++){
            printf("%c", matrix[row][column]);
        }
        putchar('\n');
    }


    //print out encrypted string
    char *s = (char*)malloc(sizeof(char) * stringLength - spaces);
    
    row = 0;
    column = 0;
    i = 0;
    printf("Encrypted string: %s\n", s);    
    for(row = 0; row < 10; row++){
        for(column = 0; column < 10; column++){
            if(matrix[row][column]!=' ' && matrix[row][column]!='\n'){
                *(s + i) = matrix[row][column];    
                i++;    
            }
        }
    }

    printf("Encrypted string: %s\n", s);
    return 0;

}

Our next program is a simple text formatter. To do this, we must determine how many lines will fit on a single line of text, and then we must figure out how many blank spaces we must include to make sure that the line fills the margins.

#include <stdio.h>
#include <string.h>

#define EXPAND_LINE 1
#define NO_EXPAND 0
#define WIDTH 50

void initializeBuffer(char *buff);
void loadWord(char *buffer, char *word, int *spaceremaining);
int getWord(char *text, int index, char *word);
void printLine(char *buffer);

int main(void){

    char *text = "A lot hinges on the fact that, in most circumstances, people are not allowed to hit you with a mallet. They put up all kinds of visible and invisible signs that say 'Do not do this' in the hope that it'll work, but if it doesn't, then they shrug, because there is, really, no mallet at all.";


    char buffer[100];
    char word[100];

    int spaceleft, nextIndex, currIndex;

    spaceleft = WIDTH;

    nextIndex = 0;

    initializeBuffer(buffer);

    while(1){
        //get() function returns int value
        //indicating where to resume search in text
        //side effect is loading next word into word
        //buffer
        nextIndex = getWord(text, currIndex, word);
        if(spaceleft >= strlen(word)){
            //loadword side effects
            //decrements spaceleft
            //loads word into buffer
            loadWord(buffer, word, &spaceleft);
            currIndex = nextIndex;
        } else {
            //print out line
            printLine(buffer);
            //reset buffer
            initializeBuffer(buffer);
            //reset spaceleft
            spaceleft = WIDTH;
        }
        //break out of loop if
        //no more words
        if(strlen(word)==0){
            if(buffer[0]!=''){
                printLine(buffer);
            }
            break;
        }
        
    } // end while loop

    return 0;

}

void initializeBuffer(char *buffer){
    for(int i = 0; i < WIDTH; i++){
        *(buffer+i) = '';
    }
}

int getWord(char *text, int index, char *word){
    int i = 0;
    
    while((text[index]==' ') && (text[index] != '')){
        index++;
    }

    //copy characters into word
    while((text[index]!=' ') && (text[index] != ''))
    {
        *(word+i) = text[index];
        i++;
        index++;
    }
    *(word+i) = '';

    return index;
}

void loadWord(char *buffer, char *word, int *spaceremaining){
    strcat(buffer, word);
    *spaceremaining -= strlen(word);
    if(WIDTH > strlen(buffer)){
        strcat(buffer, " ");
        (*spaceremaining)--;
    }
}


void printLine(char *buffer){

    int index = 0;
    int wordEndIndex  = 0;
    int copyIndex;
    //no need to expand the line
    if(strlen(buffer)==WIDTH){
        printf("%s\n", buffer);
    } else {
        int blanks = WIDTH - strlen(buffer);
    
        while(blanks-- > 0){
            //find end of word
            while(buffer[wordEndIndex]!=' ' & buffer[wordEndIndex]!='\n'){
                wordEndIndex++;
            }    
            for(copyIndex = WIDTH - 1; copyIndex > wordEndIndex; copyIndex--){
                buffer[copyIndex] = buffer[copyIndex - 1];
            }
            buffer[wordEndIndex] = ' ';
            wordEndIndex++;
        }        
        //attach end of line character    
        buffer[WIDTH] = '';
        
        printf("%s\n", buffer);        
    } // end else
}

If you have any interest in learning more about C, take a look at my book at http://www.amazon.com/Big-Als-C-Standard-ebook/dp/B00A4JGE0M/

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s