简体   繁体   中英

How do you assign a char from one char array to another char array in a loop in c?

I am trying to write code that will read in morse code from one .txt file into a char array. I then read in a test file into another char array. From there I want to read the test file into morse code and print it to the monitor.

My input files are:

A.- B-... C-.- D-.. E. F.._. G--. H.... I.. J.--- K-.- L.-.. M-- N-. O--- P.--.
Q--.- R.-. S... T- U..- V...- W.-- X-..- Y-.-- Z--..

ABCDEFGHIJKLMNOPQRSTUVWXYZ

My .c code is as follows

#include <stdio.h>
#include <stdlib.h>

void main()
{
    int  i=0,j=0, n=0,x=0, SIZE=100;

    char SENTENCE[100][100];
    char morse[100][100];
    char fileName[SIZE];
    char SecondFile[SIZE];


    printf("Enter a file name\n");
    scanf("%s", fileName);

    FILE *file;  /*  FILE pointer  */
    file = fopen(fileName, "r");  /* open a text file for reading */


    while(fgets(morse[n++],20,file))
    {
    }


    for (i=0 ; i<n; i++)
    {
       printf("\n%s",  morse[i]);
    }


    printf("\n\nEnter a file name\n");
    scanf("%s", SecondFile);
    FILE *start;  /*  FILE pointer  */
    start = fopen(SecondFile, "r");  /* open a text file for reading */


    while(fgets(SENTENCE[j++],30,start))
    {
    }


    for (i=0 ; i<j; i++)
    {
       printf("\n%s",  SENTENCE[i]);
    }


    for (x=0; x<=SIZE; x++)                     
    {                                           
       if(SENTENCE[x]=='A'||SENTENCE[x]=='a')   
            printf("\n%s", morse[x]);            
       else if(SENTENCE[x]=='B'||SENTENCE[x]=='b') 
            printf("\n%s", morse[x]);
       else if(SENTENCE[x]=='C'||SENTENCE[x]=='c')
            printf("\n%s", morse[x]);
       else if(SENTENCE[x]=='D'||SENTENCE[x]=='d')
            printf("\n%s", morse[x]);
       else if(SENTENCE[x]=='E'||SENTENCE[x]=='e')
            printf("\n%s", morse[x]);
       else if(SENTENCE[x]=='F'||SENTENCE[x]=='f')
            printf("\n%s", morse[x]);
       else if(SENTENCE[x]=='G'||SENTENCE[x]=='g')
            printf("\n%s", morse[x]);
       else if(SENTENCE[x]=='H'||SENTENCE[x]=='h')
            printf("\n%s", morse[x]);
       else if(SENTENCE[x]=='I'||SENTENCE[x]=='i')
            printf("\n%s", morse[x]);
       else if(SENTENCE[x]=='J'||SENTENCE[x]=='j')
            printf("\n%s", morse[x]);
       else if(SENTENCE[x]=='K'||SENTENCE[x]=='k')
            printf("\n%s", morse[x]);
       else if(SENTENCE[x]=='L'||SENTENCE[x]=='l')
            printf("\n%s", morse[x]);
       else if(SENTENCE[x]=='M'||SENTENCE[x]=='m')
            printf("\n%s", morse[x]);
       else if(SENTENCE[x]=='N'||SENTENCE[x]=='n')
            printf("\n%s", morse[x]);
       else if(SENTENCE[x]=='O'||SENTENCE[x]=='o')
            printf("\n%s", morse[x]);
       else if(SENTENCE[x]=='P'||SENTENCE[x]=='p')
            printf("\n%s", morse[x]);
       else if(SENTENCE[x]=='Q'||SENTENCE[x]=='q')
            printf("\n%s", morse[x]);
       else if(SENTENCE[x]=='R'||SENTENCE[x]=='r')
            printf("\n%s", morse[x]);
       else if(SENTENCE[x]=='S'||SENTENCE[x]=='s')
            printf("\n%s", morse[x]);
       else if(SENTENCE[x]=='T'||SENTENCE[x]=='t')
            printf("\n%s", morse[x]);
       else if(SENTENCE[x]=='U'||SENTENCE[x]=='u')
            printf("\n%s", morse[x]);
       else if(SENTENCE[x]=='V'||SENTENCE[x]=='v')
            printf("\n%s", morse[x]);
       else if(SENTENCE[x]=='W'||SENTENCE[x]=='w')
            printf("\n%s", morse[x]);
       else if(SENTENCE[x]=='X'||SENTENCE[x]=='x')
            printf("\n%s", morse[x]);
       else if(SENTENCE[x]=='Y'||SENTENCE[x]=='y')
            printf("\n%s", morse[x]);
       else if(SENTENCE[x]=='Z'||SENTENCE[x]=='z')
            printf("\n%s", morse[x]);
       else if(SENTENCE[x]=='\0')
            printf("nothing read");
       else if(SENTENCE[x]=='.')
            printf("nothing read");
       else
        printf("error reading in a character from the file");

    }
    fclose(file);
    fclose(start);

    getch();
}

In the for loop above I'm trying to print the exact morse code equivalence. The only problem is I keep getting warnings when trying to compare the array SENTENCE[x] with a ch . Also is there anyway to assign certain characters from one char array to another char array in c. Is there a way I can do any of this? I'm currently writing the code in c.

SENTENCE[x] does not return char, it returns char[]

remember that you defined SENTENCE as char[][]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM