简体   繁体   English

如何在c中用字符串填充数组

[英]How can I fill array with string in c

I need to fill an array with a string that is input by a user.我需要用用户输入的字符串填充数组。 For example user enters such string like these: "St/80" We know the length before user enters it btw.例如,用户输入这样的字符串:“St/80”我们知道用户输入之前的长度 btw。 I wanna do this:我想这样做:

    array[0]='S';
    array[1]='t';
    array[2]='/';
    array[3]=8;
    array[4]=0;

Look at the code below:看看下面的代码:

#include <stdio.h>
int main(void)
{
    char arr[10];
    scanf("%9s", arr);
    printf("%s",arr);
    return 0;
}

here if your input string is "St/80" then it will assign as below:在这里,如果您的输入字符串是“St/80”,那么它将分配如下:

array[0]='S';
array[1]='t';
array[2]='/';
array[3]=8;
array[4]=0;

also you can increase the size of string by increasing the index of arr.您也可以通过增加 arr 的索引来增加字符串的大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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