当前位置:网站首页>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;
}
边栏推荐
- Neon Optimization: an instruction optimization case of matrix transpose
- THREE.AxesHelper is not a constructor
- 2022 Google CTF SEGFAULT LABYRINTH wp
- table表格设置圆角
- Tensorflow GPU installation
- 黑马笔记---创建不可变集合与Stream流
- Force buckle 1037 Effective boomerang
- NEON优化:log10函数的优化案例
- from . cv2 import * ImportError: libGL. so. 1: cannot open shared object file: No such file or direc
- 1123. 最深叶节点的最近公共祖先
猜你喜欢
字节P7专业级讲解:接口测试常用工具及测试方法,福利文
[HFCTF2020]BabyUpload session解析引擎
黑马笔记---创建不可变集合与Stream流
Windows installation mysql8 (5 minutes)
[Niuke] b-complete square
Installation of gazebo & connection with ROS
[Niuke] [noip2015] jumping stone
Dynamic planning idea "from getting started to giving up"
Gazebo的安装&与ROS的连接
Let's see through the network i/o model from beginning to end
随机推荐
Go zero micro service practical series (IX. ultimate optimization of seckill performance)
黑马笔记---异常处理
Using the entry level of DVA in taro3.*
Transformation transformation operator
Make a simple graphical interface with Tkinter
Taro2.* applet configuration sharing wechat circle of friends
Informatics Orsay Ibn YBT 1172: find the factorial of n within 10000 | 1.6 14: find the factorial of n within 10000
树莓派/arm设备上安装火狐Firefox浏览器
boot - prometheus-push gateway 使用
HMM 笔记
LLDP兼容CDP功能配置
[batch dos-cmd command - summary and summary] - string search, search, and filter commands (find, findstr), and the difference and discrimination between find and findstr
Asset security issues or constraints on the development of the encryption industry, risk control + compliance has become the key to breaking the platform
2022 Google CTF SEGFAULT LABYRINTH wp
7.6模拟赛总结
斗地主游戏的案例开发
BFS realizes breadth first traversal of adjacency matrix (with examples)
Neon Optimization: an optimization case of log10 function
Openjudge noi 1.7 10: simple password
[100 cases of JVM tuning practice] 05 - Method area tuning practice (Part 2)