take char input and store it in an array

2020-05-06 10:33发布

I want to take n number of inputs and save it in the arrays c[] and p[] and later use them...

I have currently written this,but i'm not getting desired output

#include<stdio.h>
#include<stdlib.h>
int main()
{
    int n,t,i,j,size=0;
    char s[100000];
    char c[100];
    char p[100];
    scanf("%d", &n);

    for(i=0;i<n;i++)
    {
        scanf("%c", &c[i]);
        scanf("%c", &p[i]);
    }

    for(i=0; i<n;i++)
    {
        printf("%c %c", c[i],p[i]);
    }
    return 0;
}

3条回答
姐就是有狂的资本
2楼-- · 2020-05-06 11:13

Considering from your second comment: "i want it to be like 4 w r 2 9 f g q t now c should store w2fq and p should store r9gt ",
you should change all the for(...) loop with for(i=0;i<n/2;i++)

查看更多
冷血范
3楼-- · 2020-05-06 11:30
for(i=0;i<n;i++)
{
    scanf(" %c", &c[i]);//skip space character
    scanf(" %c", &p[i]);
}
查看更多
Summer. ? 凉城
4楼-- · 2020-05-06 11:32

Use this

for(i=0; i<n;i++)
{
    scanf("%c %c", &c[i], &p[i]);
}

%s is for string of characters. If you want to read only one character, you should use %c.

查看更多
登录 后发表回答