当前位置:网站首页>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;
}
边栏推荐
- The difference between spin and sleep
- Data type of pytorch tensor
- 斗地主游戏的案例开发
- THREE. AxesHelper is not a constructor
- Informatics Olympiad YBT 1171: factors of large integers | 1.6 13: factors of large integers
- Analysis of mutex principle in golang
- 736. Lisp 语法解析 : DFS 模拟题
- Maidong Internet won the bid of Beijing life insurance to boost customers' brand value
- table表格设置圆角
- taro3.*中使用 dva 入门级别的哦
猜你喜欢
Analysis of mutex principle in golang
Do you understand this patch of the interface control devaxpress WinForms skin editor?
LLDP兼容CDP功能配置
Let's see through the network i/o model from beginning to end
golang中的Mutex原理解析
2022 Google CTF segfault Labyrinth WP
The MySQL database in Alibaba cloud was attacked, and finally the data was found
[Niuke] b-complete square
ARM裸板调试之JTAG原理
405 method not allowed appears when the third party jumps to the website
随机推荐
Dynamic planning idea "from getting started to giving up"
微信公众号发送模板消息
NEON优化:性能优化常见问题QA
【信号与系统】
Typical problems of subnet division and super network construction
MySQL中回表的代价
LLDP兼容CDP功能配置
Go zero micro service practical series (IX. ultimate optimization of seckill performance)
gnet: 一个轻量级且高性能的 Go 网络框架 使用笔记
JTAG principle of arm bare board debugging
Make Jar, Not War
Supersocket 1.6 creates a simple socket server with message length in the header
Yunna | work order management measures, how to carry out work order management
[Niuke] [noip2015] jumping stone
Dark horse notes - create immutable sets and streams
资产安全问题或制约加密行业发展 风控+合规成为平台破局关键
taro3.*中使用 dva 入门级别的哦
MySQL script batch queries all tables containing specified field types in the database
【C语言进阶篇】指针的8道笔试题
Transplant DAC chip mcp4725 to nuc980