From ce1d2ae68f137243af1f0f61f1da9c45ebc278d4 Mon Sep 17 00:00:00 2001 From: flykhan Date: Sun, 25 Jun 2023 16:59:08 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9A=E4=B9=89=E5=87=BD=E6=95=B0,=20?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E5=AD=97=E7=AC=A6=E4=B8=B2=E4=B8=AD=E6=9F=90?= =?UTF-8?q?=E4=B8=AA=E5=AD=97=E7=AC=A6=E7=9A=84=E6=9F=A5=E6=89=BE,=20?= =?UTF-8?q?=E5=B9=B6=E8=BF=94=E5=9B=9E=E5=AD=97=E7=AC=A6=E7=9A=84=E4=B8=8B?= =?UTF-8?q?=E6=A0=87=E6=88=96=20-1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- day9/case5.cpp | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 day9/case5.cpp diff --git a/day9/case5.cpp b/day9/case5.cpp new file mode 100644 index 0000000..41a297e --- /dev/null +++ b/day9/case5.cpp @@ -0,0 +1,26 @@ +// 定义函数, 实现字符串中某个字符的查找, 并返回字符的下标或 -1 +#include + +using namespace std; +int findCharAt(char str[], int size, char ch) +{ + for (int i = 0; i < size; i++) + { + if (str[i] == ch) + return i; + else if (str[i] == '\0' && str[i] != ch) + return -1; + } +} + +int main() +{ + char str[] = "abcdefg"; + char ch; + cout << "请输入要查找的字符: "; + cin >> ch; + int size = sizeof(str) / sizeof(str[0]); + int index = findCharAt(str, size, ch); + cout << "下标为: " << index << endl; + return 0; +} \ No newline at end of file