当前位置:网站首页>Wrong question (character array)

Wrong question (character array)

2022-06-11 02:00:00 Jihai

1. Insert string

Title Description : Enter two strings a and b, take b The largest character in the string is inserted into a After the smallest character in the string .

The sample input : MynameisAmy

       MynameisJane

Sample output : MynameisAymy

Tips : The string length does not exceed 100

#include <stdio.h>
#include <string.h>
int main()
{
    /*********Begin*********/
char a[100],b[100];
int i,j,m,n,max,min;
gets(a);
gets(b);
m=strlen(a);
n=strlen(b);
a[min]=a[0];
for(i=0;i<m;i++){
    if(a[min]>=a[i]) min=i;
}
for(i=0;i<=min;i++){
    printf("%c",a[i]);
}
b[max]=b[0];
for(i=0;i<n;i++){

    if(b[max]<=b[i]) max=i;
}
printf("%c",b[max]);
for(i=min+1;i<m;i++){
    printf("%c",a[i]);
}
    /*********End**********/
    return 0;
}

2. Character statistics

For a given string , Count the number of times the number of characters appear .

Input There are multiple lines of input data , The first line is an integer n, Indicates the number of test instances , Follow behind n That's ok , Each line contains a string of letters and numbers .

Output For each test case , Output the number of values in the string , One line per output .

The sample input : 2asdfasdf123123asdfasdf

        asdf111111111asdfasdfasdf

Sample output : 6

       9

#include<stdio.h>
int main(void)
{
    /*********Begin*********/
	int n,i,j,count;
	char s[10][50];   
	scanf("%d",&n);
	for(i=0;i<n;i++)
	{
	scanf("%s",&s[i]);
	}

	for(i=0;i<n;i++)        
	{
		count=0;          
		for(j=0;s[i][j]!='\0';j++)       
			if(s[i][j]>='0'&&s[i][j]<='9')
				count++;                
		printf("%d\n",count);
	}
    /*********End**********/
    return 0;
}

3. Input multidimensional character array by line with pointer

Title Description : Input 3 That's ok , Each row n A string , Output in order of small to large

The sample input : cde

       afg

      abc

Sample output : abc

       afg

       cde

#include<stdio.h>
#include<string.h>
int main(void)
{
    /*********Begin*********/
char str[3][100];
	int i,j,k;
	for(k=0;k<3;k++)
	gets(str[k]);
	char *ps[3]={str[0], str[1],str[2]};
	char *temp;
	
	for(i=0;i<3;i++)
	{
		for(j=i+1;j<3;j++)
		{
			if(strcmp(ps[i],ps[j])>0)
			{
				temp=ps[i];
				ps[i]=ps[j];
				ps[j]=temp;
			}
		}
	}
 
	for(i=0;i<3;i++)
		printf("%s\n",ps[i]);
    /*********End**********/
    return 0;
}

原网站

版权声明
本文为[Jihai]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203020622358498.html