输入字符串str和字符ch,输出ch在str中最后一次出现的位置;若没出现,则输出0。字符串长度不超过50。
输入格式:
输入包括两行。
第一行输入字符串str
第二行输入字符ch
输出格式:
输出ch在str中出现的最后一次,若没有则输出0.
输入样例:
asdfghj123
3
输出样例:
10
下面代码有一个问题没有办法,实现做到“输出ch在str中出现的最后一次,若没有则输出0”。
输入样例:
#include <stdio.h>
#include <string.h>
int main()
{
char str[51], ch;
int len, i, last = 0;
// 输入字符串和字符
scanf("%s", str);
scanf(" %c", &ch);
// 获取字符串长度
len = strlen(str);
// 查找字符
for (i = 0; i < len; i++)
{
if (str[i] == ch)
{
last = i + 1; // 记录位置
}
}
// 输出最后一次出现的位置或0
printf("%d\n", last);
return 0;
}