qfedu-basic-level/day9/case4.cpp

32 lines
623 B
C++
Raw Permalink Normal View History

2023-06-26 08:34:05 +08:00
// 定义函数, 实现字符串中指定位置字符删除
#include <iostream>
using namespace std;
void delChar(char str[], int index)
{
int i = index;
while (str[i] != '\0')
{
str[i] = str[i + 1];
i++;
}
}
int main()
{
char str[] = "abcdefg";
int index;
cout << "原字符串: " << str << endl;
cout << "请输入要删除的位置: ";
cin >> index;
if (index < 0 || index > sizeof(str) / sizeof(str[0]))
{
cout << "位置不合法" << endl;
return 0;
}
delChar(str, index);
cout << "删除后: " << str << endl;
return 0;
}