当前位置:网站首页>C language instance_ three
C language instance_ three
2022-07-07 01:25:00 【Vicky__ three thousand and twenty-one】
1. Target deletion
The function of the given program is to delete strings ( A character array )s All lowercase letters in c. Please correct the mistakes in the procedure , So that it can get the right result .
#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. Remove whitespace
Programming , The function is to delete all spaces in the input string . The string length does not exceed 30.
Input :
One line of characters .
Output :
Print out the result string after removing the space character .
# 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. String manipulation
Programming , The function is to convert the source string s All characters with odd subscripts or ASCII code Put new characters for even characters ( strand ) Array t in ( Specify that the first character be placed on the 0 In a ). And output the new string after processing .
Input :
One line of characters , The length is less than 20.
Output :
Processed string .
#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. Find the longest string
Programming , The function is to input the keyboard N Find the longest string in the string and output it .
Input :
The first line is an integer N, Represents the number of strings .
The second line starts with N A string .
Output :
The longest string .
#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]);// Input
}
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]);// Output the longest string
return 0;
}
5. Word order
There is an array of strings containing 9 A string , Each string is a word , And the length of each string shall not exceed 7, It is required to sort the words before outputting . The specific requirements are as follows :
- Use a two-dimensional character array to store this 9 A string .
- Enter data for a two-dimensional character array .
- Output pre - and post - processing string arrays and compare them .
Input :
9 A string , The length is less than 7.
Output :
Before printout processing 9 A string .
After printout processing 9 A string .
# include <stdio.h>
# include <string.h>
int main ()
{
char str[9][10];
for (int i = 0 ; i < 9 ; i++) scanf("%s",str[i]);
printf(" String before sorting :\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)// Use function to judge size
{
char tmp[10];// Define a temporary array to exchange
strcpy(tmp,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],tmp);
}
}
}
printf(" Sorted string :\n");
for (int i = 0 ; i < 9 ; i++) printf("%s\n",str[i]);
return 0;
}
边栏推荐
- Neon Optimization: performance optimization FAQ QA
- 交叉验证如何防止过拟合
- gnet: 一个轻量级且高性能的 Go 网络框架 使用笔记
- AI automatically generates annotation documents from code
- 身体质量指数程序,入门写死的小程序项目
- How to evaluate load balancing performance parameters?
- Body mass index program, entry to write dead applet project
- 【案例分享】网络环路检测基本功能配置
- NEON优化:log10函数的优化案例
- 阿里云中mysql数据库被攻击了,最终数据找回来了
猜你喜欢
JTAG debugging experience of arm bare board debugging
如何管理分布式团队?
Dark horse notes - create immutable sets and streams
ARM裸板调试之JTAG原理
[signal and system]
2022 Google CTF SEGFAULT LABYRINTH wp
Let's see through the network i/o model from beginning to end
C language - array
LeetCode:1175. 质数排列
Force buckle 1037 Effective boomerang
随机推荐
机器学习:随机梯度下降(SGD)与梯度下降(GD)的区别与代码实现。
What are the differences between Oracle Linux and CentOS?
Transformation transformation operator
go-zero微服务实战系列(九、极致优化秒杀性能)
自旋与sleep的区别
docker 方法安装mysql
资产安全问题或制约加密行业发展 风控+合规成为平台破局关键
Match VIM from zero (0) -- Introduction to vimscript
Lldp compatible CDP function configuration
NEON优化:log10函数的优化案例
Installation of torch and torch vision in pytorch
剑指 Offer II 035. 最小时间差-快速排序加数据转换
Tensorflow GPU installation
Add the applet "lazycodeloading": "requiredcomponents" in taro,
[100 cases of JVM tuning practice] 05 - Method area tuning practice (Part 2)
1123. The nearest common ancestor of the deepest leaf node
Niuke cold training camp 6B (Freund has no green name level)
分享一个通用的so动态库的编译方法
今日问题-2022/7/4 lambda体中修改String引用类型变量
golang中的Mutex原理解析