当前位置:网站首页>面试算法 - 字符串问题总结
面试算法 - 字符串问题总结
2022-06-24 18:32:00 【学习&笔记】
1.怎样将整数转化为字符串,并且不用函数itoa.
解析:整数转化为字符串,可以采用加‘0’,再逆序的办法 整数加‘0’就会隐性转化成char类型
的数。
#include <stdio.h>
int main(void)
{
int num=12345, j=0, i=0;
char temp[7], str[7];
while(num)
{
temp[i]=num%10+'0';
i++;
num=num/10;
}
temp[i]=0;
printf("temp=%s\n",temp);
i=i-1;
printf("temp=%d\n",i);
//刚刚转化的字符串是逆序的,必须把它反转过来
while(i>=0)
{
str[j]=temp[i];
j++;
i--;
}
str[j]=0;
printf("string=%s\n",str);
return 0;
}
2.字符串转化成整数,可以采用减‘0’再乘10累加的办法,字符串减‘0’就会隐性转化成int类型
的数。
#include <stdio.h>
int main(void)
{
int num = 12345, j=0, i=0, sum=0;
char temp[7]={'1','2','3','4','5','\0'}, str[7];
while(temp[i])
{
sum=sum*10+(temp[i]-'0');
i++;
}
printf("sum=%d\n",sum);
return 0;
}
3.输入一行字符串,找出其中出现的相同且长度最长的字符串,输出它及其首字符的位置,
例如“yyabcdabjcabceg”,输出结果应该为abc和3.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str,tep;
cout << "请输入字符串" << endl;
cin >> str;
for(int i=str,lenth()-1;i>1;i++)
{
for(int j=0;j<str.length();j++)
{
if(j+1<=str.length())
{
size_t t=0;
size_t num=0;
tep=str.substr(j,i); //从大到小取子串
t=str.find(tep); //正序查找
num=str.rfind(tep); //逆序查找
if(t!=num) //如果两次查找位置不一致说明存在重复子串
{
cout<<tep<<" "<<t+1<<endl; //输出子串及位置
return 0;
}
}
}
}
return 0;
}
4.编写程序从键盘输入若干小写字母,输出这些字符的大写字母。
小写字母的'a'~'z'的ASCII值是97~122,大写字母的'A'~'Z'的ASCII值是65~90,相差32.
#include "stdio.h"
int main(void)
{
char a, b, c, d, i; //定义5个字符型变量
printf("请输入4个小写字母:\n");
scanf("%c%c%c%c",&a,&b,&c,&d); //用户依次接收4个小写字母。
i = 'a' - 'A';
printf("对应的大写字母是:%c%c%c%c\n",a-i,b-i,c-i,d-i); //输出大写
return 0;
}
/运行的结果:
//请输入4个小写字母:
//abcd
//对应的大写字母是:ABCD
5.计算一个字节里(byte)里面有多少bit被置1
#include <stdio.h>
int comb(const int c)
{
int cout = 0;
int i = 0;
int cc = c;
while(i++<8)
{
if((cc&1)==1)
{
count++;
}
cc = cc>>1;
}
return count;
}
int main()
{
const int c = 0xcf;
printf("%d\n",comb(c));
return 1;
}
6.将一个字符串逆序
#include <stdio.h>
#include <string.h>
char *strconv(char *p)
{
int length = strlen(p);
char *ptr = p;
char *ptr_1 = p + length - 1;
while(ptr < ptr_1)
{
char c = *ptr;
*ptr = *ptr_1;
*ptr_1 = c;
++ptr;
--ptr_1;
}
return p;
}
int main()
{
char str[] = "asffdsgg";
char *p;
p = strconv(str);
printf("%s",p);
return 1;
}
7.接收一个人的英文名字字符,输出其存储的内容(使用字符串)
#include "stdio.h"
#include "string.h"
int bin_no_recursion(char name);
int main()
{
char name[20]; //数组定义,定义一个存储20个字符的数组。
int bin,i; //定义变量
printf("请输入你的名字:"); //提示用户输入一个字符串
gets(name); //接收一个字符
i=0;
printf("在计算机里的存储是");
while(name[i]!='\0')
{
bin=bin_no_recursion(name[i]); //调用函数
printf("\n二进制 %d.",bin); //输出结果
i++;
}
return 0;
}
int bin_no_recursion(char name)
{
int k,bin; //定义变量
int i;
k = 1;
bin = 0; //赋初值
i = 1;
while(name!=0) //整数不为0进行循环
{
bin=bin+name%2*k; //将二进制的最低位累加到bin
name = name/2; //整数除以2
k = k*10;
i++;
}
return bin;
}
8.字符串连接函数strcat()
#include "stdio.h"
#include "string.h"
int main()
{
char strl[30] = "I am"; //定义字符串数组1
char str2[10] = "a student"; //定义字符串数组2
strcat(str1,str2); //调用系统提供的字符串连接函数
puts(str1); //输出连接以后的结果
return 0;
}
//输出结果是:I am a student
9.编写程序输入一个字符串和一个单个字符,在输入的字符中删除所有的单个字符
#include "stdio.h"
#include "string.h"
int main()
{
int i,j;
char str1[80],c;
puts("请输入一个字符串:");
gets(str1); //读入第一个字符串
puts("请输入一个字符串:");
c = getchar(); //读入单个字符
puts("删除前:");
putc(str1); //输出删除字符之前的字符串
i = 0;
while(str1[i]!='\0') //删除与字符c相等的所有字符
{
if(str[i]==c)
{
for(j=i;str1[j]!='\0';j++)
str1[j]=str1[j+1];
}
if(str1[i]!=c) i++; //当前字符不是c, i加1;
}
puts("删除后"); //输出删除字符之后的字符串
puts(str1);
return 0;
}
10.编写函数形式参数为字符数组,对该字符数组存储的字符串中的字符从小到大排序并输出。
#include "stdio.h"
#define SIZE 80
void sort(char str[])
{
int i,j,min_t;
char temp;
for(i=0;str[i]!='\0';i++) //对str数组进行排序
{
min_t=i;
for(j=i+1;str[j]!='\0';j++)
if(str[j]<str[min_t])
min_t=j;
temp=str[min_t];
str[min_t] = str[i];
str[i] = temp;
}
}
int main()
{
char str1[SIZE];
puts("请输入一个字符串:");
gets(str1); //读入一个字符串
puts("排序前");
puts(str1); //输出去排序以前的字符串
sort(str1); //调用排序函数
puts("排序后");
puts(str1); //输出排序以后的字符串
}
11.编写函数,函数有两个字符串类型的形参,将在第一个字符串中出现的但在第二个字符串未出现的字符存放在第三个数组中并输出。
例如:第一个字符串是"ABACDEFGH" 第二个字符串是"BCD" 则第三个字符串是"AAEFGH"
#include "stdio.h"
#define SIZE 80
void pro(char str1[], char str2[], char str3[])
{
int i,j,k,flag;
k = 0;
for(i = 0;str1[i]!='\0';i++)
{
flag=1;
for(j=0;str2[j]!='\0'&&flag;j++)
if(str2[j]==str1[i]) flag=0; //第一个字符串的字符在第二个字符串中存在,则flag赋值为0.
if(flag) //flag未被赋值为0,说明第一个字符串中字符在第二个字符串中不存在
{
str3[k]=str1[i];
k++;
}
}
str3[k] = '\0';
}
int main()
{
char str1[SIZE], str2[SIZE], str3[SIZE];
puts("请输入第一个字符串:");
gets(str1); //读入第一个字符串
puts("请输入第二个字符串:");
gets(str2); //读入第二个字符串
pro(str1,strr2,str3); //生成第三个字符串
puts("第三个字符串是:");
puts(str3); //输出第三个字符串
return 0;
}
边栏推荐
- 股票网上开户安全吗?应该怎么办理?
- Network security database penetration of secondary vocational group in 2022
- Complete Guide to web application penetration testing
- Analysis on the issue of raising the right of MSSQL in 2021 secondary vocational group network security competition in Shandong Province
- EasyGBS视频平台TCP主动模式拉流异常情况修复
- 你知道CMDB吗?
- Application service access configuration parameters
- ASP. Net hosting uploading file message 500 error in IIS
- Data driven decision making: Decision intelligence and design thinking
- Is it safe to open an account online? What should I do?
猜你喜欢
Get max value of a bit column - get max value of a bit column
Redis learning -- list of redis operations
[quick news] the jeecgboot low code platform was successfully selected into the 2021 scientific innovation China · open source innovation list
Why are more and more people studying for doctors? Isn't it more and more difficult to graduate a doctor?
Skills of writing test cases efficiently
Five skills of selecting embedded programming language
Crmeb multi merchant PC packaging tutorial
Eight recommended microservice testing tools
What is decision intelligence?
How to start cloud native application development
随机推荐
Three years of bug free, tips for improving code quality
Why should state-owned enterprises accelerate the digital transformation
A solution to the problem that the separator of WordPress title - is escaped as -
Wechat applet to realize stacked rotation
中电投先融期货这家公司怎么样?期货开户办理安全吗?
Project Management Guide: tips, strategies and specific practices
Application service access configuration parameters
Nine practical guidelines for improving responsive design testing
He "painted" what a smart city should look like with his oars
Sword finger offer 10- ii Frog jumping on steps
Gateway solves cross domain access
Number of occurrences of numbers in the array (medium difficulty)
Uncover the secrets of Tencent R & D! 30% of the demand will be responded within 1 day!
What if the database table structure changes? Smartbi products support one click synchronization
Optimizing bloom filter: challenges, solutions, and comparisons
Complete Guide to web application penetration testing
Selection (030) - what is the output of the following code?
【你真的会用ES吗】ES基础介绍(一)
Ten software development indicators for project managers
布隆过滤器综述文章论文阅读:Optimizing Bloom Filter: Challenges, Solutions, and Comparisons