[C] Does the code you wrote match what is required?

Asked 1 years ago, Updated 1 years ago, 279 views

プログラミングI'm a beginner in programming. I think there are many things that I can't do, but please be gentle.

I wonder if the code I wrote understands the title of the assignment?
The results are correct.

There are two things that I am concerned about, so I would like you to tell me.
·When calling a function under condition 4
str_dist_a (str, aa, ba);str_dist_p (str, ap, bp);
However, str could not be defined, so I couldn't call it, so I stepped on it as a mistake and placed it as s.Is this judgment wrong?(Whether you can write the ideal code while keeping =str)

·The temporary argument is the pointer function str_dist_p and the left side looks like an array.In my perception, I used the nature of being able to use it like a matrix by attaching [] to the pointer. Is this correct?

The following code

#include<stdio.h>

void str_dist_a(char str[], char x[], char y[]);
void str_dist_p(char*str, char*x, char*y);

void str_dist_a(char str[], char x[], char y[]) // x = even, y = odd
{
    inti,ix,iy;

    ix = 0; iy = 0;

    for (i=0; str[i]!='\0'; i++)
    {
        if(i%2==0)// [even]
        {
            x[ix] = str[i];
            ix++;
        }
        else//[odd]
        {
            y[iy] = str[i];
            iy++;
        }
    }
    x[ix] = '\0'; y[iy] = '\0';
}

void str_dist_p(char*str, char*x, char*y)//x=even,y=add
{
    inti,ix,iy;

    ix = 0; iy = 0;

    for(i=0;*(str+i)!='\0'; i++)
    {
        if(i%2==0)// [even]
        {
            x[ix]=*(str+i); // Arrayy on the left
            ix++;
        }
        else//[odd]
        {
            y[iy]=*(str+i); // Arrayy on the left
            iy++;
        }
    }
    x[ix]='\0';y[iy]='\0';// Looks like an array on the left
}


int main (void)
{
    chars[120];
    charaa[64]; charba[64]; charap[64]; charbp[64];

    printf("String:");scanf("%s",s);

    str_dist_a(s,aa,ba);

    printf("Distribution string in array 1=%s\n", aa);
    printf("Distribution string in array 2=%s\n",ba);

    str_dist_p(s,ap,bp);

    printf("Distribution string at pointer 1=%s\n", ap);
    printf("Distribution string at pointer 2=%s\n",bp);

    return 0;
}

/*
string:baapnpalnea
Distribution string in array 1 = banana
Array Distribution String 2 = Apple
Distribution string at pointer 1 = banana
Pointer distribution string 2 = apple

*/

That's the code

Challenge Contents

I admit that the code is quite hard to read and hard to see.
I'm sorry.

If there is a shortage of common sense parts (function name, variable name, etc.)
I would appreciate it if you could point that out as well.

c pointer

2022-12-09 02:09

1 Answers

·When calling a function under condition 4
str_dist_a(str, aa, ba); str_dist_p(str, ap, bp);
However, I couldn't call it because str was not defined, so I stepped on it as a mistake and put it as s.Is this judgment wrong?(Whether or not you can write the ideal code while keeping =str)

If you add char*str=s;, you can write a code that satisfies the title, but
It's probably the fault of the problem side.
If you're worried, contact the questioner.

·The temporary argument is the pointer function str_dist_p, and the left side looks like an array.In my perception, I used the nature of being able to use it like a matrix by attaching [] to the pointer. Is this correct?

Probably
*(x+ix)=*(str+i);
It's meant to be written.

There is a context in which the task is presented, so from an irrelevant point of view, it is only a complete guess.
In this case, it means to treat it as an array.
a[i] is synonymous with *(a+i) and
There's no such thing as "treat as an array."


2022-12-09 02:34

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.