1.字符串反转
实现字符串反转函数。例如,“July"反转后变成"yluJ”。
#include<stdio.h>
#include<string.h>
#include<vector>
#include<iostream>
using namespace std;
int main()
{
vector<char>a;
char c;
while (1)
{
cin >> c;
if (c == '&')
{
break;
}
a.push_back(c);
}
for (int i = a.size()-1; i >=0; i--)
{
cout << a[i];
}
cout << endl;
return 0;
}
2.字符的左右移动
给定一个字符串,这个字符串为‘*号和26个字母的任意组合。现在需要把字符串中的 /*号都移动到最左侧,而把字符串中的字母移到最右侧并保持相对顺序不变,要求时间复杂度和空间复杂度最小。
#include<stdio.h>
#include<string.h>
#include<vector>
#include<iostream>
using namespace std;
int main()
{
vector<char>a;
char c;
while (1)
{
cin >> c;
if (c == '&')
{
break;
}
a.push_back(c);
}
for (int i = a.size()-1; i >=0; i--)//这一层的循环是寻找字符数组中的*号的位置,从后往前
{
if (a[i] - '*' == 0)
{
int j;
for (j = i; j >= 0; j--)//这一层循环是找i之前的第一个字母
{
if (a[j] - '*' != 0)
{
break;
}
}
if (j >= 0)//交换
{
char n;
n = a[i];
a[i] = a[j];
a[j] = n;
}
}
}
for (int i = 0; i < a.size(); i++)
{
cout << a[i];
}
cout << endl;
return 0;
}
3.字符个数的统计
给定一个字符串,写一个函数,查找出该字符串中每个字符出现的次数,要求区分大小写,且时间复杂度为O(n)。
#include<stdio.h>
#include<string.h>
#include<vector>
#include<iostream>
using namespace std;
int main()
{
vector<char>a;//输入的字符串
vector<int>b(128,0);//acsll码值的数组,哪个字符对应哪个acsll码值,哪个ascll码值就加1,初始化全为0
char c;
while (1)
{
cin >> c;
if (c == '&')
{
break;
}
a.push_back(c);
}
char s,n;
for (int i = 0; i < a.size(); i++)
{
b[(int)a[i]] += 1;
}
for (int i = 0; i < b.size(); i++)
{
if (b[i] != 0)
{
printf("%c:%d\n", i, b[i]);
}
}
return 0;
}
4.字符串的匹配
在一篇英文文章中查找指定的人名,人名使用26个英文字母(可以是大写或小写)、空格及两个通配符(和?)组成。通配符表示零个或多个任意字母,通配符?表示一个任意字母。例如,"J* Smi??“可以匹配"John Smith”。
#include <iostream>
using namespace std;
bool match(char *str1, char* str2)
{
if (*str1 == '\0' && *str2 == '\0') //递归
return true;
else if (*str1 == '\0' || *str2 == '\0') //递归
return false;
if (*str1 == '?') //匹配1个字符
return match(str1 + 1, str2 + 1);
else if (*str1 == '*') //匹配0个或以上的字符
return match(str1 + 1, str2) || match(str1 + 1, str2 + 1) || match(str1, str2 + 1); //分别代表匹配0个,1个和n个(递归)
else if (*str1 == *str2) //通配符的优先级别比较高
return match(str1 + 1, str2 + 1);
return false; //无通配符且两字符不等
}
int main()
{
char str1[100], str2[100]; //该题实参用字符数组名,形参用指针,比较合适
while (cin >> str1 >> str2)
{
if (match(str1, str2))
cout << "true" << endl;
else
cout << "false" << endl;
}
return 0;
}
5.字符串空格的压缩
给定一个字符串,将其中连续出现的空格压缩为1个后,将其中以空格分隔的每个字符串逆序打印出来。例如,“There are moments in life"的打印结果为"erehT era stnemom ni efil”。
#include<stdio.h>
#include<string.h>
#include<vector>
#include<iostream>
using namespace std;
void fun_2(char* a, int min, int max,int l)//将摘取出来的单词逆序输出
{
if (min == -1 || max == -1)//判断英文句子的开头是否有空格
{
}
for (int j = max; j > min - 1; j--)
{
printf("%c", a[j]);
}
if (max != l - 1)//判断最后一个单词
{
printf(" ");
}
}
void fun_1(char* a, int l)
{
int min = -1, max = -1, f = 1;
for (int i = 0; i < l; i++)
{
if (f == 1 && a[i] != ' ')
{
min = i;
f = 0;
}
else
{
max = i;
if ((a[i + 1] == ' '&&f == 0) || max == l - 1)
{
fun_2(a, min, max,l);
min = -1;
max = -1;
f = 1;
}
}
}
}
int main()
{
char a[100] = "There are moments in life";
int l = strlen(a);
fun_1(a,l);//将每一个单词摘取出来
return 0;
}
6.重复字符的压缩
通过键盘输入一串小写字母(a~z) 组成的字符串。请编写一个字符串压缩程序,对字符串中连续出现的重复字母进行压缩,并按要求输出压缩后的字符串。具体压缩规则是:仅压缩连续重复出现的字符。例如,字符串"abcbc"由于无连续重复字符,压缩后的字符串还是"abcbc"。压缩后输出的格式要求为有重复的字符按“字符重复的次数+字符”输出,无格式的字符原样输出。例如,字符串"xxyyyyyz"压缩后输出为"3x6yz",字符串"ccdecc"压缩后输出为"3c2de2c",字符串"adef"压缩后输出为"adef",字符串"pppppp"压缩后输出为"8p"。
#include<stdio.h>
#include<string.h>
int main()
{
char a[100];
int n[26][2] = {0};//第一列存储a-z个字符,按字符串中的顺序存,第二列存储每一个字符的个数
int s1,s2=0;//s2记录a串中的字符种类数,s1是记录当前遍历的字符
gets(a);
int s = strlen(a);
s1 = a[0] - 'a';
for (int i = 0; i <= s; i++)
{
if (a[i] == s1 + 'a')
{
n[s2][0] = s1;
n[s2][1] += 1;
}
else
{
if (n[s2][1] == 1)//当只有一个字符时,就输出本身
{
printf("%c", n[s2][0] + 'a');
}
else//当字符连续重复时,就输出个数加字符
{
printf("%d%c", n[s2][1], n[s2][0]+'a');
}
s1 = a[i] - 'a';
s2++;
n[s2][0] = s1;
n[s2][1] += 1;
}
}
printf("\n");
return 0;
}
7.在字符串s中找出第一个只出现次的字符,如果没有这个单空幅、格,s只包含小写字母。
#include<stdio.h>
#include<string.h>
#include <map>
#include<iostream>
using namespace std;
char fun(char* s)
{
map<char, int> m;
if (&s == ' ')
return ' ';
for (int i = 0; i < strlen(s); i++)
{
m[s[i]] += 1;
}
for (int i = 0; i < strlen(s); i++)
{
if (m[s[i]] == 1)
{
return s[i];
}
}
}
int main()
{
char s[50001];
gets(s);
printf("%c\n",fun(s));
return 0;
}