当前位置:网站首页>C语言实例_3
C语言实例_3
2022-07-06 17:39:00 【Vicky__3021】
1.目标删除
给出的程序的功能是删除字符串(字符数组)s中所有的小写字母c。请改正程序中的错误,使它能得到正确的结果。
#include "stdio.h"
#include "string.h"
int main()
{
char s[80];
int i,j;
scanf("%s",s);
printf("The original string: \n");
puts(s);
for(i=j=0;s[i]!='\0';i++)
{
if(s[i]!= 'c')
s[j++]=s[i];
}
s[j]='\0';
printf("The string after deleted: \n");
puts(s);
return 0;
}
2.去除空格符
编写程序,功能是删除输入的字符串中的所有空格。字符串长度不超过30。
输入:
一行字符。
输出:
打印输出去除空格符后的结果字符串。
# include "stdio.h"
# include "string.h"
int main()
{
char word[31];
scanf("%[^\n]", word);
char *p;
p = word;
int i, j;
for(i=0; i<31; i++){
if(*p == ' '){
for(j=i; j<31; j++){
word[j] = word[j+1];
}
}
p++;
}
puts(word);
scanf("%d", &i);
return 0;
}
3.字符串操作
编写程序,功能是将源字符串s中所有下标为奇数的字符或 ASCII码 为偶数的字符放入新字符(串)数组t中(规定第一个字符放在第0位中)。 并输出处理后新的字符串。
输入:
一行字符,长度小于20.
输出:
处理后的字符串。
#include <stdio.h>
#include <string.h>
#define N 80
main()
{
char s[N], t[N];
int i,j=0,k=0;
gets(s);
while(s[j]) j++;
for(i=0;i<j;i++)
if(i%2||s[i]%2==0)
t[k++]=s[i];
t[k]=0;
printf("%s\n",t);
}
4.找最长字符串
编写程序,功能是将键盘输入的N个字符串中找出最长的那个串并输出。
输入:
第一行为一个整数N,代表字符串个数。
第二行开始输入N个字符串。
输出:
最长的字符串。
#include <stdio.h>
#include <string.h>
int main()
{
int N;
char s[100][100];
int i;
int max_i, max_len = 0;
scanf("%d",&N);
for(i = 0; i < N; i ++)
{
scanf("%s",s[i]);//输入
}
for(i = 0; i < N; i ++)
{
int l = strlen(s[i]);
if(max_len <l)
{
max_len = l;
max_i = i;
}
}
printf("%s\n", s[max_i]);//输出最长字符串
return 0;
}
5.单词排序
有一个字符串数组包含9个字符串,每个字符串都是一个单词,且每个串的长度均不超过7,要求对单词进行排序后再输出。具体要求如下:
- 使用二维字符数组存放这9个字符串。
- 为二维字符数组输入数据。
- 输出处理前和处理后的字符串数组并进行比较。
输入:
9个字符串,长度小于7。
输出:
打印输出处理前的9个字符串。
打印输出处理后的9个字符串。
# include <stdio.h>
# include <string.h>
int main ()
{
char str[9][10];
for (int i = 0 ; i < 9 ; i++) scanf("%s",str[i]);
printf("排序前的字符串:\n");
for (int i = 0 ; i < 9 ; i++) printf("%s\n",str[i]);
for (int i = 0 ; i < 8 ; i++)
{
for (int j = i+1 ; j < 9 ; j++)
{
if (strcmp(str[i],str[j])>0)// 利用函数来判断大小
{
char tmp[10];// 定义临时数组用于交换
strcpy(tmp,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],tmp);
}
}
}
printf("排序后的字符串:\n");
for (int i = 0 ; i < 9 ; i++) printf("%s\n",str[i]);
return 0;
}
边栏推荐
- Your cache folder contains root-owned files, due to a bug in npm ERR! previous versions of npm which
- 树莓派/arm设备上安装火狐Firefox浏览器
- Come on, don't spread it out. Fashion cloud secretly takes you to collect "cloud" wool, and then secretly builds a personal website to be the king of scrolls, hehe
- 2022 Google CTF segfault Labyrinth WP
- C # method of calculating lunar calendar date 2022
- Gazebo的安装&与ROS的连接
- Installation of gazebo & connection with ROS
- How to evaluate load balancing performance parameters?
- ARM裸板调试之JTAG调试体验
- Meet in the middle
猜你喜欢

Dark horse notes - exception handling

1123. 最深叶节点的最近公共祖先
![[Niuke] [noip2015] jumping stone](/img/9f/b48f3c504e511e79935a481b15045e.png)
[Niuke] [noip2015] jumping stone

云呐|工单管理办法,如何开展工单管理

如何管理分布式团队?

移植DAC芯片MCP4725驱动到NUC980

Niuke cold training camp 6B (Freund has no green name level)

ARM裸板调试之JTAG调试体验

【C语言进阶篇】指针的8道笔试题
![[100 cases of JVM tuning practice] 05 - Method area tuning practice (Part 2)](/img/40/dc45df3cd3ee7642277eff899bc6aa.png)
[100 cases of JVM tuning practice] 05 - Method area tuning practice (Part 2)
随机推荐
Pytorch中torch和torchvision的安装
Niuke cold training camp 6B (Freund has no green name level)
剑指 Offer II 035. 最小时间差-快速排序加数据转换
table表格设置圆角
Maidong Internet won the bid of Beijing life insurance to boost customers' brand value
mysql: error while loading shared libraries: libtinfo.so.5: cannot open shared object file: No such
【C语言进阶篇】指针的8道笔试题
界面控件DevExpress WinForms皮肤编辑器的这个补丁,你了解了吗?
[hfctf2020]babyupload session parsing engine
"Exquisite store manager" youth entrepreneurship incubation camp - the first phase of Shunde market has been successfully completed!
免费白嫖的图床对比
自旋与sleep的区别
[100 cases of JVM tuning practice] 05 - Method area tuning practice (Part 2)
斗地主游戏的案例开发
ARM裸板调试之JTAG原理
docker 方法安装mysql
云呐|工单管理办法,如何开展工单管理
How to manage distributed teams?
Spark TPCDS Data Gen
The difference between spin and sleep