100 cases of classic C language (31-40)

Posted by davidsakh on Sat, 30 Oct 2021 23:09:03 +0200

Source of all questions: Rookie tutorial C language classic 100 cases

Attach the previous title: 100 cases of classic C language (21-30)

catalogue

C exercise example 31
C exercise example 32
C exercise example 33
C exercise example 34
C exercise example 35
C exercise example 36 - find prime numbers within 100
C exercise example 37 - sorting
C exercise example 38
C exercise example 39
C exercise example 40

C exercise example 31

Title:
Please enter the first letter of the day of the week to judge the day of the week. If the first letter is the same, continue to judge the second letter.

Program analysis: it is better to use the situation statement. If the first letter is the same, judge the second letter with the situation statement or if statement.

example

//  Created by www.runoob.com on 15/11/9.
//  Copyright  ©  All rights reserved
//
 
#include<stdio.h>
 
int main()
{
    char i,j;
    printf("Please enter the first letter:\n");
    scanf("%c",&i);
    getchar();//scanf("%c",&j); The second time is to read in a newline character instead of the input character, so you need to add a getchar() to replace the newline character
    switch(i)
    {
        case 'm':
            printf("monday\n");
            break;
        case 'w':
            printf("wednesday\n");
            break;
        case 'f':
            printf("friday\n");
            break;
        case 't':
            printf("Please enter the next letter\n");
            scanf("%c",&j);
            if (j=='u') {printf("tuesday\n");break;}
            if (j=='h') {printf("thursday\n");break;}
        case 's':
            printf("Please enter the next letter\n");
            scanf("%c",&j);
            if (j=='a') {printf("saturday\n");break;}
            if (j=='u') {printf("sunday\n"); break;}
        default :
            printf("error\n"); break;
    }
    return 0;
}

The output results of the above examples are:

Please enter the first letter:
s
Please enter the next letter
a
saturday

C exercise example 32

Title:
Delete the specified letter in a string, such as the string "aca", and delete the letter A.

Program analysis: none.
example

//  Created by www.runoob.com on 15/11/9.
//  Copyright  ©  All rights reserved
//
 
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
 
// Deletes the specified letter function from the string
char* deleteCharacters(char * str, char * charSet)
{
    int hash [256];
    if(NULL == charSet)
        return str;
    for(int i = 0; i < 256; i++)
        hash[i] = 0;
    for(int i = 0; i < strlen(charSet); i++)
        hash[charSet[i]] = 1;
    int currentIndex = 0;
    for(int i = 0; i < strlen(str); i++)
    {
        if(!hash[str[i]])
            str[currentIndex++] = str[i];
    }
    str[currentIndex] = '\0';
    return str;
}
 
int main()
{
    char s[2] = "a";     // Letter to delete
    char s2[5] = "aca";  // Target string
    printf("%s\n", deleteCharacters(s2, s));
    return 0;
}

The output results of the above examples are:

c

C exercise example 33

Title:
Judge whether a number is a prime number.

Program analysis: prime number, also known as prime number, has infinite numbers. A natural number greater than 1 cannot be divided by other natural numbers except 1 and itself.

Program source code:

//  Created by www.runoob.com on 15/11/9.
//  Copyright  ©  All rights reserved
//
 
#include<stdio.h>
#include<math.h>
#define MAX 1000
 
 
int prime[MAX];
 
int isPrimeNaive(int n)
{
    if(n <= 1)
        return 0;
    for(int i = 2; i < n; i++)
        if(n % i == 0)
            return 0;
    return 1;
}
 
int isPrime(int n)
{
    if(n<= 1)
        return 0;
    if(n == 2)
        return 1;
    if(n%2 == 0)
        return 0;
    int limit = (int)sqrt((double)n);
    for(int i = 3; i <= limit; i=i+2)
    {
        if(n % i == 0)
            return 0;
    }
    return 1;
}
 
void sieve()
{
    prime[0] = 0;
    prime[1] = 0;
    for(int i = 2; i < MAX; i++)
        prime[i] = 1;
    int limit = (int)sqrt((double)MAX);
    for(int i = 2; i <= limit; i++)
    {
        if(prime[i])
            for(int j = i*i; j <= MAX; j+=i)
                prime[j] = 0;
    }
}
 
int isPrimeSieve(int n)
{
    if(prime[n])
        return 1;
    else
        return 0;
}
 
int main()
{
    sieve();
    printf("N=%d %d\n", 1, isPrime(1));
    printf("N=%d %d\n", 2, isPrime(2));
    printf("N=%d %d\n", 3, isPrime(3));
    printf("N=%d %d\n", 4, isPrime(4));
    printf("N=%d %d\n", 7, isPrime(7));
    printf("N=%d %d\n", 9, isPrime(9));
    printf("N=%d %d\n", 13, isPrime(13));
    printf("N=%d %d\n", 17, isPrime(17));
    printf("N=%d %d\n", 100, isPrime(100));
    printf("N=%d %d\n", 23, isPrime(23));
    printf("N=%d %d\n", 1, isPrime(1));
    return 0;
}

The output result of the above example is (the last number 1 indicates that it is a prime number, and 0 indicates that it is not a prime number):

N=1 0
N=2 1
N=3 1
N=4 0
N=7 1
N=9 0
N=13 1
N=17 1
N=100 0
N=23 1
N=1 0

C exercise example 34

Title:
Practice function calls.

Program analysis: none.

Program source code:

//  Created by www.runoob.com on 15/11/9.
//  Copyright  ©  All rights reserved
//
 
#include <stdio.h>
void hello_world(void)
{
    printf("Hello, world!\n");
}
void three_hellos(void)
{
    int counter;
    for (counter = 1; counter <= 3; counter++)
        hello_world();/*Call this function*/
}
int main(void)
{
    three_hellos();/*Call this function*/
}

The output results of the above examples are:

Hello, world!
Hello, world!
Hello, world!

C exercise example 35

Title:
String inversion, such as reversing the string "www.runoob.com" to "moc.boonur.www".

Program analysis: none.

example

//  Created by www.runoob.com on 15/11/9.
//  Copyright  ©  All rights reserved
//
 
#include <stdio.h>
 
 
void reverse(char* s)
{
    // Get string length
    int len = 0;
    char* p = s;
    while (*p != 0)
    {
        len++;
        p++;
    }
    
    // Swap
    int i = 0;
    char c;
    while (i <= len / 2 - 1)
    {
        c = *(s + i);
        *(s + i) = *(s + len - 1 - i);
        *(s + len - 1 - i) = c;
        i++;
    }
}
 
int main()
{
    char s[] = "www.runoob.com";
    printf("'%s' =>\n", s);
    reverse(s);           // Reverse string
    printf("'%s'\n", s);
    return 0;
}

The output results of the above examples are:

'www.runoob.com' =>
'moc.boonur.www'

C exercise example 36 - find prime numbers within 100

Title:
Find the prime within 100.

Program analysis: prime number, also known as prime number, has infinite numbers. A natural number greater than 1 cannot be divided by other natural numbers except 1 and itself.

Program source code:

//  Created by www.runoob.com on 15/11/9.
//  Copyright  ©  All rights reserved
//
 
#include<stdio.h>
#include<math.h>
int main()
{
    int i,j,k,n=0;
    for(i=2;i<=100;i++)
    {
        k=(int)sqrt(i);
        for(j=2;j<=k;j++)
            if(i%j==0) break;
        if(j>k)
        {
            printf("%d ",i);
            n++;
            if(n%5==0)
                printf("\n");
        }
    }
    return 0;
}

The output results of the above examples are:

2 3 5 7 11
13 17 19 23 29
31 37 41 43 47
53 59 61 67 71
73 79 83 89 97

C exercise example 37 - sorting

Title:
Sort 10 numbers.

Program analysis: the selection method can be used, that is, from the last 9 comparison processes, select the smallest one to exchange with the first element, and so on next time, that is, use the second element to compare with the last 8 and exchange.

example

//  Created by www.runoob.com on 15/11/9.
//  Copyright  ©  All rights reserved
//
 
#include<stdio.h>
#define N 10
int main()
{
    int i,j,a[N],temp;
    printf("Please enter 10 numbers:\n");
    for(i=0;i<N;i++)
        scanf("%d",&a[i]);
    for(i=0;i<N-1;i++)
    {
        int min=i;
        for(j=i+1;j<N;j++)
            if(a[min]>a[j]) min=j;
        if(min!=i)
        {
            temp=a[min];
            a[min]=a[i];
            a[i]=temp;
        }
    }
    printf("The sorting result is:\n");
    for(i=0;i<N;i++)
        printf("%d ",a[i]);
    printf("\n");
    return 0;
}

The output results of the above examples are:

Please enter 10 numbers:
23 2 27 98 234 1 4 90 88 34
The sorting result is:
1 2 4 23 27 34 88 90 98 234

C exercise example 38

Title:
Find the sum of diagonal elements of a 3 * 3 matrix

Program analysis: double for loop control is used to input two-dimensional array, and then a[i][i] is accumulated and output.

example

//  Created by www.runoob.com on 15/11/9.
//  Copyright  ©  All rights reserved
//
 
#include<stdio.h>
#define N 3
int main()
{
    int i,j,a[N][N],sum=0;
    printf("Please enter a matrix(3*3): \n");
    for(i=0;i<N;i++)
        for(j=0;j<N;j++)
            scanf("%d",&a[i][j]);
    for(i=0;i<N;i++)
        sum+=a[i][i];
    printf("The sum of diagonal lines is:%d\n",sum);
    return 0;
}

The output results of the above examples are:

Please enter the matrix (3 * 3):
1 2 3
4 5 6
7 8 9
Sum of diagonal lines: 15

C exercise example 39

Title:
There is an ordered array. Now enter a number and ask to insert it into the array according to the original law.

Program analysis: first judge whether this number is greater than the last number, and then consider inserting the number in the middle. After inserting, the number after this element will move back one position in turn.

example

//  Created by www.runoob.com on 15/11/9.
//  Copyright  ©  All rights reserved
//
 
#include<stdio.h>
int main()
{
    int a[11]={1,4,6,9,13,16,19,28,40,100};
    int temp1,temp2,number,end,i,j;
    printf("The original array is:\n");
    for(i=0;i<10;i++)
        printf("%4d",a[i]);
    printf("\n Insert a new number: ");
    scanf("%d",&number);
    end=a[9];
    if(number>end)
        a[10]=number;
    else
    {
        for(i=0;i<10;i++)
        {
            if(a[i]>number)
            {
                temp1=a[i];
                a[i]=number;
                for(j=i+1;j<11;j++)
                {
                    temp2=a[j];
                    a[j]=temp1;
                    temp1=temp2;
                }
                break;
            }
        }
    }
    for(i=0;i<11;i++)
        printf("%4d",a[i]);
    printf("\n");
    return 0;
}

The output results of the above examples are:

The original array is:
1 4 6 9 13 16 19 28 40 100
Insert a new number: 10
1 4 6 9 10 13 16 19 28 40 100

C exercise example 40

Title:
Output an array in reverse order.

Program analysis: exchange the first with the last.

Program source code:

//  Created by www.runoob.com on 15/11/9.
//  Copyright  ©  All rights reserved
//
 
#include<stdio.h>
#define N 10
int main()
{
    int a[N]={0,1,2,3,4,5,6,7,8,9};
    int i,t;
    printf("The original array is:\n");
    for(i=0;i<N;i++)
        printf("%d ",a[i]);
    for(i=0;i<N/2;i++)
    {
        t=a[i];
        a[i]=a[N-1-i];
        a[N-1-i]=t;
    }
    printf("\n Sorted array:\n");
    for(i=0;i<N;i++)
        printf("%d ",a[i]);
    printf("\n");
    return 0;
}

The output results of the above examples are:

The original array is:
0 1 2 3 4 5 6 7 8 9
Sorted array:
9 8 7 6 5 4 3 2 1 0

Topics: C Back-end