编写字符串反转函数。该函数的功能是将指定字符串中的字符顺序颠倒存放,然后在主函数中进行测试。
#include<iostream>
using namespace std;
int match(char s[], char p[])
{
int i, j, k;
for (i = 0; s[i] != '\0'; i++)
{
for (j = i, k = 0; s[j] != '\0' && p[k] == s[j]; j++, k++);
if (p[k] == '\0')
return i;
}
return -1;
}
int main()
{
char s1[100], s2[100]; //创建一个字符数组
cout << "请输入字符串A: " << endl;
cin.getline(s1, 100);
cout << "请输入子字符串B: " << endl;
cin.getline(s2, 100);
int k = match(s1, s2);
if (k != -1)
cout << s2 << "是" << s1 << "的子字符串,位置在" << k + 1 << endl;
else
cout << s2 << "不是" << s1 << "的子字符串";
system("pause");
return 0;
}
运行截图: