当前位置:网站首页>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: an optimization case of log10 function
- 斗地主游戏的案例开发
- Taro applet enables wxml code compression
- What does security capability mean? What are the protection capabilities of different levels of ISO?
- Gazebo的安装&与ROS的连接
- [chip scheme design] pulse oximeter
- Implementation principle of waitgroup in golang
- Niuke cold training camp 6B (Freund has no green name level)
- Wood extraction in Halcon
- Google发布安全更新,修复Chrome中已被利用的0 day
猜你喜欢
Lldp compatible CDP function configuration
系统休眠文件可以删除吗 系统休眠文件怎么删除
Force buckle 1037 Effective boomerang
JTAG principle of arm bare board debugging
[hfctf2020]babyupload session parsing engine
Niuke cold training camp 6B (Freund has no green name level)
[signal and system]
1123. 最深叶节点的最近公共祖先
boot - prometheus-push gateway 使用
Wood extraction in Halcon
随机推荐
2022 Google CTF segfault Labyrinth WP
table表格设置圆角
Do you understand this patch of the interface control devaxpress WinForms skin editor?
2022 Google CTF SEGFAULT LABYRINTH wp
系统休眠文件可以删除吗 系统休眠文件怎么删除
剑指 Offer II 035. 最小时间差-快速排序加数据转换
Vocabulary in Data Book
C语言实例_2
ARM裸板调试之JTAG原理
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
golang中的atomic,以及CAS操作
Send template message via wechat official account
2022 Google CTF SEGFAULT LABYRINTH wp
JTAG principle of arm bare board debugging
Dark horse notes - exception handling
Segmenttree
boot - prometheus-push gateway 使用
斗地主游戏的案例开发
数据手册中的词汇
Grc: personal information protection law, personal privacy, corporate risk compliance governance