Need help either turning a section of code into a function

First off, thanks for trying ot help me (potentially). I have a simple program which ciphers text using a 2D array. You specify what letters to replace with what and it does it. However, I would like to convert a section of code into a function on its own but I keep getting random errors like: expected char*... or out of scope.

I would be grateful if you could guide me on the correct path.

Thanks again

Code:

#include <conio.h> #include <stdio.h> // Main loop     int main() {         // 2D array - column 0 == original - column 1 == changed - init char array to 0 to get rid of garbage         char repl_letter[26][2] = {             {0},             {0}         };         // User input to be ciphered         char userinput[10];         // Ask user for key         int replace_alpha;         // Characters in array - user input         int characters;         int counter;         // Count of characters - user input         int count;         // Alphabet         repl_letter[0][0] = 'a';         repl_letter[1][0] = 'b';         repl_letter[2][0] = 'c';         repl_letter[3][0] = 'd';         repl_letter[4][0] = 'e';         repl_letter[5][0] = 'f';         repl_letter[6][0] = 'g';         repl_letter[7][0] = 'h';         repl_letter[8][0] = 'i';         repl_letter[9][0] = 'j';         repl_letter[10][0] = 'k';         repl_letter[11][0] = 'l';         repl_letter[12][0] = 'm';         repl_letter[13][0] = 'n';         repl_letter[14][0] = 'o';         repl_letter[15][0] = 'p';         repl_letter[16][0] = 'q';         repl_letter[17][0] = 'r';         repl_letter[18][0] = 's';         repl_letter[19][0] = 't';         repl_letter[20][0] = 'u';         repl_letter[21][0] = 'v';         repl_letter[22][0] = 'w';         repl_letter[23][0] = 'x';         repl_letter[24][0] = 'y';         repl_letter[25][0] = 'z';         // User input - string         printf("What should I cipher?");         // User input         characters = getchar();         // Count of characters         count = 0;         // Track array size and avoid buffer overflow         while ((count < 10) && (characters != EOF)) {             // Next character             userinput[count] = characters;             // Increment count of characters             ++count;             // Get another character             characters = getchar();         }         // Ask user for the key to cipher - loop through the alphabet - nth term         for (replace_alpha = 0; replace_alpha < 26; replace_alpha++) {             // Ask and use replace_alpha             printf("Enter repl_letter for alph %d ", replace_alpha);             // use %c for single character and put space before to omit newline - stored in column 1 of 2D array             scanf(" %c", & repl_letter[replace_alpha][1]);         }         // REPLACING CHARACTERS        for ( counter = 0; counter < 26; counter++ ) {             if (repl_letter[counter][0] == userinput[0]) {                 userinput[0] = repl_letter[counter][1];             }         }         for ( counter = 0; counter < 26; counter++ ) {             if (repl_letter[counter][0] == userinput[1]) {                 userinput[1] = repl_letter[counter][1];             }         }         for ( counter = 0; counter < 26; counter++ ) {             if (repl_letter[counter][0] == userinput[2]) {                 userinput[2] = repl_letter[counter][1];             }         }         for ( counter = 0; counter < 26; counter++ ) {             if (repl_letter[counter][0] == userinput[3]) {                 userinput[3] = repl_letter[counter][1];             }         }         for ( counter = 0; counter < 26; counter++ ) {             if (repl_letter[counter][0] == userinput[4]) {                 userinput[4] = repl_letter[counter][1];             }         }         for ( counter = 0; counter < 26; counter++ ) {             if (repl_letter[counter][0] == userinput[5]) {                 userinput[5] = repl_letter[counter][1];             }         }         for ( counter = 0; counter < 26; counter++ ) {             if (repl_letter[counter][0] == userinput[6]) {                 userinput[6] = repl_letter[counter][1];             }         }         for ( counter = 0; counter < 26; counter++ ) {             if (repl_letter[counter][0] == userinput[7]) {                 userinput[7] = repl_letter[counter][1];             }         }         for ( counter = 0; counter < 26; counter++ ) {             if (repl_letter[counter][0] == userinput[8]) {                 userinput[8] = repl_letter[counter][1];             }         }         for ( counter = 0; counter < 26; counter++ ) {             if (repl_letter[counter][0] == userinput[9]) {                 userinput[9] = repl_letter[counter][1];             }         }         for ( counter = 0; counter < 26; counter++ ) {             if (repl_letter[counter][0] == userinput[10]) {                 userinput[10] = repl_letter[counter][1];             }         }       printf(userinput);         // Pause at the end of program for user convenience         getch();         return 0;     } 

This part:

    // REPLACING CHARACTERS    for ( counter = 0; counter < 26; counter++ ) {         if (repl_letter[counter][0] == userinput[0]) {             userinput[0] = repl_letter[counter][1];         }     }     for ( counter = 0; counter < 26; counter++ ) {         if (repl_letter[counter][0] == userinput[1]) {             userinput[1] = repl_letter[counter][1];         }     }     for ( counter = 0; counter < 26; counter++ ) {         if (repl_letter[counter][0] == userinput[2]) {             userinput[2] = repl_letter[counter][1];         }     }     for ( counter = 0; counter < 26; counter++ ) {         if (repl_letter[counter][0] == userinput[3]) {             userinput[3] = repl_letter[counter][1];         }     }     for ( counter = 0; counter < 26; counter++ ) {         if (repl_letter[counter][0] == userinput[4]) {             userinput[4] = repl_letter[counter][1];         }     }     for ( counter = 0; counter < 26; counter++ ) {         if (repl_letter[counter][0] == userinput[5]) {             userinput[5] = repl_letter[counter][1];         }     }     for ( counter = 0; counter < 26; counter++ ) {         if (repl_letter[counter][0] == userinput[6]) {             userinput[6] = repl_letter[counter][1];         }     }     for ( counter = 0; counter < 26; counter++ ) {         if (repl_letter[counter][0] == userinput[7]) {             userinput[7] = repl_letter[counter][1];         }     }     for ( counter = 0; counter < 26; counter++ ) {         if (repl_letter[counter][0] == userinput[8]) {             userinput[8] = repl_letter[counter][1];         }     }     for ( counter = 0; counter < 26; counter++ ) {         if (repl_letter[counter][0] == userinput[9]) {             userinput[9] = repl_letter[counter][1];         }     }     for ( counter = 0; counter < 26; counter++ ) {         if (repl_letter[counter][0] == userinput[10]) {             userinput[10] = repl_letter[counter][1];         }     } 

I tried something like this but it didn't work :

#include <conio.h> #include <stdio.h> void charreplace(int numberofelement, char userinput, char repl_letter[]); // Main loop     int main() {         // 2D array - column 0 == original - column 1 == changed - init char array to 0 to get rid of garbage         char repl_letter[26][2] = {             {0},             {0}         };         // User input to be ciphered         char userinput[10];         // Ask user for key         int replace_alpha;         // Characters in array - user input         int characters;         // Count of characters - user input         int count;         // Alphabet         repl_letter[0][0] = 'a';         repl_letter[1][0] = 'b';         repl_letter[2][0] = 'c';         repl_letter[3][0] = 'd';         repl_letter[4][0] = 'e';         repl_letter[5][0] = 'f';         repl_letter[6][0] = 'g';         repl_letter[7][0] = 'h';         repl_letter[8][0] = 'i';         repl_letter[9][0] = 'j';         repl_letter[10][0] = 'k';         repl_letter[11][0] = 'l';         repl_letter[12][0] = 'm';         repl_letter[13][0] = 'n';         repl_letter[14][0] = 'o';         repl_letter[15][0] = 'p';         repl_letter[16][0] = 'q';         repl_letter[17][0] = 'r';         repl_letter[18][0] = 's';         repl_letter[19][0] = 't';         repl_letter[20][0] = 'u';         repl_letter[21][0] = 'v';         repl_letter[22][0] = 'w';         repl_letter[23][0] = 'x';         repl_letter[24][0] = 'y';         repl_letter[25][0] = 'z';         // User input - string         printf("What should I cipher?");         // User input         characters = getchar();         // Count of characters         count = 0;         // Track array size and avoid buffer overflow         while ((count < 10) && (characters != EOF)) {             // Next character             userinput[count] = characters;             // Increment count of characters             ++count;             // Get another character             characters = getchar();         }         // Ask user for the key to cipher - loop through the alphabet - nth term         for (replace_alpha = 0; replace_alpha < 26; replace_alpha++) {             // Ask and use replace_alpha             printf("Enter repl_letter for alph %d ", replace_alpha);             // use %c for single character and put space before to omit newline - stored in column 1 of 2D array             scanf(" %c", & repl_letter[replace_alpha][1]);         }         // REPLACING CHARACTERS        charreplace(0, userinput, repl_letter[26,2]);        charreplace(1, userinput, repl_letter[26,2]);        charreplace(2, userinput, repl_letter[26,2]);        charreplace(3, userinput, repl_letter[26,2]);        charreplace(4, userinput, repl_letter[26,2]);        charreplace(5, userinput, repl_letter[26,2]);        charreplace(6, userinput, repl_letter[26,2]);        charreplace(7, userinput, repl_letter[26,2]);        charreplace(8, userinput, repl_letter[26,2]);        charreplace(9, userinput, repl_letter[26,2]);        printf(userinput);         // Pause at the end of program for user convenience         getch();         return 0;     }     void charreplace (int numberofelement, char userinput, char repl_letter[]) {         int counter;         for ( counter = 0; counter < 26; counter++ ) {             if (repl_letter[counter][0] == userinput[numberofelement]) {                 userinput[numberofelement] = repl_letter[counter][1];             }         }     } 

Replay

Category: algorithm Time: 2016-07-31 Views: 0

Related post

iOS development

Android development

Python development

JAVA development

Development language

PHP development

Ruby development

search

Front-end development

Database

development tools

Open Platform

Javascript development

.NET development

cloud computing

server

Copyright (C) avrocks.com, All Rights Reserved.

processed in 0.168 (s). 12 q(s)