当前位置:网站首页>面试算法 - 字符串问题总结
面试算法 - 字符串问题总结
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 student9.编写程序输入一个字符串和一个单个字符,在输入的字符中删除所有的单个字符
#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;
}边栏推荐
- Crmeb multi merchant PC packaging tutorial
- 国家出手了!对知网启动网络安全审查
- Flutter dart regular regexp special characters $, () (IV)
- How does the video platform import the old database into the new database?
- Design topic: MATLAB UAV flight operation
- Wechat applet to realize stacked rotation
- System Verilog - randomize
- Paper sharing | self supervised learning paper jointly released by Yann Lecun and read by engineers
- On software requirement analysis
- EasyPlayer流媒体播放器播放HLS视频,起播速度慢的技术优化
猜你喜欢

Mengyou Technology: tiktok current limiting? Teach you to create popular copywriting + popular background music selection

Three layer switching experiment
Business based precipitation component = & gt; manage-table

About pyqt5 to realize paging function (one window implements different interfaces)
![[NLP] 3 papers on how Stanford team builds a better chat AI](/img/f1/1c2ff31a728152395618800600df45.jpg)
[NLP] 3 papers on how Stanford team builds a better chat AI

How does the chief information security officer discuss network security with the enterprise board of directors

Software testing methods: a short guide to quality assurance (QA) models
Paper sharing | self supervised learning paper jointly released by Yann Lecun and read by engineers
congratulate! The first dragon lizard community annual outstanding contribution award is announced. Check it now
R language Quantitative Ecology redundancy analysis RDA analysis plant diversity species data visualization
随机推荐
Using flex to implement common layouts
How to select the best test cases for automation?
TCE入围2020年工信部信创典型解决方案
How does the video platform import the old database into the new database?
专有云TCE COS新一代存储引擎YottaStore介绍
[untitled]
Five advantages and disadvantages of Bi
Redis learning -- list of redis operations
(Video + graphics) introduction to machine learning series - Chapter 11 support vector machines
Rapidssl getting started SSL certificate
Ten software development indicators for project managers
Continue to help enterprises' digital transformation -tce has obtained the certification of the first batch of digital trusted service platforms in China
TCE was shortlisted as a typical solution for ICT innovation of the Ministry of industry and information technology in 2020
Bigdecimalavoiddoubleconstructorrule: do not directly use the double variable as a parameter to construct BigDecimal
Knowledge points of 2022 system integration project management engineer examination: ITSS information technology service
System Verilog - randomize
The mixed calculation of rpx and PX in JS by the uniapp applet
Four security issues of low code and no code development
Design topic: MATLAB UAV flight operation
Flutter dart regular regexp matches non printing characters \cl\cj\cm\ck