Альтернативные символы из входных строк с помощью указателей - PullRequest
0 голосов
/ 25 марта 2020

Таким образом, для этого кода он будет считывать 2 строки за раз из текстового файла, он будет принимать первый символ из строки 1 и помещать в первый адрес вывода, второй символ в строке 2 во второй адрес вывода и так далее. , Часть, в которой я борюсь, сравнивает, является ли строка 1 длиннее, чем после того, как код закончил чередование символов, тогда она напечатает остаток строки 1. Пример текстового файла:

ABCDE

PQRSTFG

acegikmoqsuwyz

bdfhjlnprtvx

Пример вывода:

APBQCRDSETFG

abcdefghijklmnopqrstuvwxyz

ниже и ниже t распечатать остаток более длинной строки.

#include <stdio.h>
int main()
{
    FILE *fp;                              /* open file pointer */
    int a,b;                               /* initialize variable a b for counter*/
    fp= fopen("cp4in_1.txt","r");          /* Open cp4in_1.txt file */
    char str1[20],str2[20];                /* initalize the size of str1 and str2 to 20 */
    char *s1,*s2,*s;                       /* create pointers s1,s2,s */
    char str[20+20+1];                     /* initialize the final string size to be 40 plus 1                                             for null */
    while ( fgets(str1, 20, fp) != NULL )  /* read in string */
    {
        fgets(str2, 20, fp);

    s1 = str1;                             /* copy address of str1 to pointers s1*/
    s2 = str2;                             /* copy address of str2 to pointers s2*/
    s = str;                               /* copy address of str to pointers s*/
    a = 0;
    b = 0;
    while((*(s1 + a)!= '\n')&&(*(s2 + b)!='\n'))       /* if the character of string 1 and                                                         string 2 is not a null character then                                                    we will alternate character */
    {
        *(s + a + b) =*(s1 + a);                       /*if a and b is 0 then s allocate the                                                  first element of s1*/
        *(s + a + b + 1) = *(s2 + b);                  /*if a and b is 0 then s allocate the                                                  first element of s2*/
        a = a + 1;                                     /* increment a and b to increment the                                                   address of s,s1 and s2*/
        b = b + 1;
    }

    if(*s1 == '\0' ||*(s1+1)=='\n')                    /* if the first string reach a NULL                                                         character or the end of line before                                                      second string*/
    {
        while(*s2!='\0')
        {
            *s++=*s2++;                                 /*increment the address of s and s2*/
            s++;                                        /*increment the element of s*/
            s2++;                                       /*increment the element of s2*/
        }
    }
    else if(*s2 == '\0'||*(s2+1)=='\n')                 /* if the first string reach a NULL                                                         character or the end of line before                                                      second string*/
        while(*s1 !='\0')
        {
            *s++=*s1++;
            s++;
            s1++;
        }
        printf("%s \n",str);
    }
    fclose(fp);                                          /* closing the text file */
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...