当前位置:网站首页>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 :

  1. Use a two-dimensional character array to store this 9 A string .
  2. Enter data for a two-dimensional character array .
  3. 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;
}
原网站

版权声明
本文为[Vicky__ three thousand and twenty-one]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207061739025858.html