简体   繁体   English

显示用户输入的字符串字符。 C语言

[英]show the character of string, that user entered. C language

I'd like to ask if it's possible to do something like this: For example I have a string B with text and I wrote a code like this: 我想问一下是否可以做这样的事情:例如,我有一个带有文本的字符串B,我写了这样的代码:

puts("Enter the number of character in string B:");

gets(number);

I would like that a program would print a character that user has entered. 我希望程序可以打印用户输入的字符。 For example 例如

char B[] = {"Shop"};

user entered 4 and computer prints p character. 用户输入4,计算机将打印p字符。 I think this should look like: 我认为这应该像:

char b[number];

printf("%s", b); 

But it doesn't work. 但这是行不通的。 I hope that you will understand what I want to do. 我希望你能理解我想做什么。 Sorry, for my English. 对不起我的英语不好。 Still learning. 还在学习。

Regard to Joe`s answer: 关于乔的回答:

Do not forget to check negative value: 不要忘记检查负值:

    if (number <= strlen(B) && unmber>=0)
        printf("%c", B[number - 1]);
    else
        printf("Entered number: %d : is out of range\n", number);

Try this: 尝试这个:

if ((number <= strlen(B)) && (number >= 1))
    printf("%c", B[number - 1]);
else
    printf("Entered number: %d : is out of range\n", number);

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

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