#include<iostream> #include<cstring> using namespace std; void fun(char *a) { int len=strlen(a); int startIndex = 0; int endIndex=len-1; while(a[startIndex]=='*'&&startIndex<len) {startIndex++;} while (a[endIndex]=='*'&&endIndex>=0) {endIndex--;} int curIndex = startIndex. for(int i=startIndex;i<=endIndex;i++) { if (a[i]!='*') {a[curIndex++]=a[i]; } } for (int i=curIndex;i<=endIndex;i++) { a[i]='*'; } a[endIndex+1]='\0';} int main() { char a[100]; cout <<"请输入一个不包含空格的带*字符串:"<< endl; cin>>a; fun(a); cout <<"处理后的字符串为:"<< endl; cout << a << endl; return 0; }
时间: 2024-04-28 09:21:46 浏览: 15
这段代码是用来处理一个带有 * 的字符串,将 * 移动到字符串的末尾。具体来说,该函数会先找到字符串开头和结尾的 *,然后将 * 之间的非 * 字符移到字符串的开头,最后将剩余的 * 移动到字符串的末尾。这个函数的时间复杂度为 O(n),其中 n 是字符串的长度。在主函数中,会先读入一个不包含空格的带 * 字符串,然后调用该函数进行处理,并输出处理后的字符串。
相关问题
c++编写一个函数,求出由指针 a 所指向的字符串中包含的每种十进制数字出现 的次数,并将统计结果保存在由指针 b 所指向的整型数组中。屏幕输出可参考下图。函数原型为: void fun1( char * a , int * b );
好的,以下是实现该功能的 C++ 代码:
```c++
#include <iostream>
#include <cstring>
using namespace std;
void fun1(char *a, int *b)
{
int len = strlen(a);
for (int i = 0; i < len; i++)
{
if (a[i] >= '0' && a[i] <= '9')
{
int num = a[i] - '0';
b[num]++;
}
}
}
int main()
{
char str[100];
int count[10] = {0};
cout << "请输入一个字符串: ";
cin >> str;
fun1(str, count);
cout << "数字出现次数如下:" << endl;
for (int i = 0; i < 10; i++)
{
cout << i << ": " << count[i] << endl;
}
return 0;
}
```
函数 `fun1` 的实现思路是遍历字符串,如果当前字符是数字,则统计该数字出现的次数。最后在主函数中调用 `fun1` 函数,输出统计结果。
注意,上述代码只能统计单个数字在字符串中出现的次数,如果要统计多位数字的出现次数,则需要对字符串进行进一步处理。
功能:编写函数void fun(char *str,char ch)从字符串str中删除指定的字符ch。 要求:本问题要求用两种算法分别实现,一是直接删除算法即查找符合删除条件的并逐次前移实现;二是间接删除算法即查找不符合删除条件的并保留下来,而保留的方法可以采用利用本数组和引入临时数组两种实现。 说明:该字符可能多次和连续出现,算法应全面考虑。要求有主函数。
直接删除算法:
```c++
#include <iostream>
#include <cstring>
using namespace std;
void fun(char *str, char ch) {
int len = strlen(str);
int i = 0, j = 0;
while (i < len) {
if (str[i] != ch) {
str[j++] = str[i];
}
i++;
}
str[j] = '\0';
}
int main() {
char str[100];
char ch;
cout << "请输入字符串:";
cin >> str;
cout << "请输入要删除的字符:";
cin >> ch;
fun(str, ch);
cout << "删除后的字符串为:" << str << endl;
return 0;
}
```
间接删除算法(利用本数组):
```c++
#include <iostream>
#include <cstring>
using namespace std;
void fun(char *str, char ch) {
int len = strlen(str);
int i = 0, j = 0;
while (i < len) {
if (str[i] != ch) {
str[j++] = str[i];
}
i++;
}
str[j] = '\0';
}
int main() {
char str[100];
char ch;
cout << "请输入字符串:";
cin >> str;
cout << "请输入要删除的字符:";
cin >> ch;
int len = strlen(str);
int i = 0, j = 0;
while (i < len) {
if (str[i] != ch) {
str[j++] = str[i];
}
i++;
}
str[j] = '\0';
cout << "删除后的字符串为:" << str << endl;
return 0;
}
```
间接删除算法(引入临时数组):
```c++
#include <iostream>
#include <cstring>
using namespace std;
void fun(char *str, char ch) {
int len = strlen(str);
char temp[100];
int i = 0, j = 0;
while (i < len) {
if (str[i] != ch) {
temp[j++] = str[i];
}
i++;
}
temp[j] = '\0';
strcpy(str, temp);
}
int main() {
char str[100];
char ch;
cout << "请输入字符串:";
cin >> str;
cout << "请输入要删除的字符:";
cin >> ch;
fun(str, ch);
cout << "删除后的字符串为:" << str << endl;
return 0;
}
```
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)