对于我的类,我们使用char数组作为字符串.如果我要使用一个if else声明,如果我修改了这样的话会是这样的吗?
我知道像这样的数组会使每个字符分解成简单的字母.并使用一个if else语句,我必须像数组[1] =='H',等等.
如果我输入"Alas",有没有办法修改下面的代码来吐出我想要的信息.现在,它只适用于该else部分.
int main()
{
char s[10];
printf("Yo, this is a string: ");
gets_s(s);
if (s == "Alas")
{
printf("B ");
}
else
{
printf("A");
}
system("pause");
}
Run Code Online (Sandbox Code Playgroud)
使用strncmp标准库函数比较两个字符串.包括<string.h>标题.
strncmp(const char *s1, const char *s2, size_t n)
Run Code Online (Sandbox Code Playgroud)
成功完成后,
strncmp()如果指向的可能以null结尾的数组s1大于,等于或小于s2分别指向的可能以null结尾的数组,则应返回大于,等于或小于0的整数.
| 归档时间: |
|
| 查看次数: |
68 次 |
| 最近记录: |