Super star MOOC advanced language programming experiment 04 array of Jilin University and its application in programming (level 2021)

1

Title No.: Exp05-Basic01, GJBook3-07-06

Title: string length

Title Description: write the program implementation: use the self-made function int strlen(char *str) to find the length of string str; And is called in the main program.
Input: randomly input a string with a length of no more than 256 from the keyboard. The string may contain several white space characters.

Output: the length of the string.
Example 1:

Input: 123    four hundred and fifty-six

Output: 8

Example 2:

Input: A

Output: 1

#include <stdio.h>
char str[256];
int strlen(char *str){
    int flag=0;
    for(int i=0;i<255;i++){   //String length is less than 255 (opportunistic) 
        if(str[i]=='\0'){
            flag=i;           //Find end 
            break;
        } 
    }
	return flag;
}
int main(){
    gets(str);
	printf("%d",strlen(str));
}

2

Title No.: Exp05-Enhanc02, freshman-1031

Title: Legal identifier

Title Description:

It is known that the definition of legal identity in C language is_ Start with the letter A continuous string of numbers and letters. Write a program to judge whether the given string is a legal C language identifier.


Input: the first line of input gives a positive integer N (≤ 10 ^ 2) is the number of input strings. Then N lines, each line gives a string of any length (may contain white space characters, and the length does not exceed 256).

Output:

The first line of output gives the number of legal identifiers X in Valid:X format, and the second line gives the number of illegal identifiers Y in Invalid:Y format. There are no redundant characters, and the colon is a Western colon.


Example:

Input:
2
1242345567788990
abcdedffesfsgfef
Output:
Valid:1
Invalid:1
​
#include <stdio.h>
#include <string.h>
char a[258];
int b[2];       //For the judgment array, b[0] meets the standard, and b[1] does not meet the standard
int pd(char *a){//If yes, it returns 1; otherwise, it returns 0
    if((a[0]!='_')&&!(a[0]<='z'&&a[0]>='a')&&!(a[0]<='Z'&&a[0]>='A')){//Judge whether the first letter meets the standard
        return 0;
	}
    for(int i=1;i<strlen(a);i++){//Then there's a cycle. There's nothing to say
        if(a[i]=='_'||(a[i]<='z'&&a[i]>='a')||(a[i]<='Z'&&a[i]>='A')||(a[i]<='9'&&a[i]>='0')){
            continue;
        }else{
		    return 0;
		}
	}
    return 1;
}
int main(){
    int n;
    scanf("%d",&n);
    getchar();          //What's the use of this getchar(), let's think about it
    for(int i=0;i<n;i++){
        gets(a);
        if(pd[a]){     
		    b[0]++;    
        }else{
            b[1]++;
        }
        for(int i=0;i<strlen(a);i++){
            a[i]='\0';
        }
    }
    printf("Valid:%d\n",b[0]);
	printf("Invalid:%d",b[1]);
}

​

3

Title No.: Exp05-Basic04, GJBook3-07-11

Title: String partial deletion

Title Description:

Programming implementation: use the self-made function char * str_delete(char *s, int v, int w) deletes w characters from the v-th character of string s, and brings the processed string first address back to the call point with the function return value.


Input:

In the first line, input two integers corresponding to v and W respectively (0 ≤ v, w < min (256, strlen (s)), and the test data ensure that v and W meet the requirements;

On the second line, enter a string with a length of no more than 256. The string may contain spaces.

Output:

Processed string.
 

Example:

Input:
2 6
ABCDEFGH 12345
Output:
AH 12345
#include <stdio.h>
#include <cstring>
char s[257];
char *s_delete(char *s,int v,int w){
    for(int i=v-1;i<strlen(s)-w;i++){   //The subscript of the v-th character is subtracted by one. Because w characters are to be deleted, it loops to
        s[i]=s[i+w];                    //strlen(s)-w is good, and then assign a new value to the deleted position
    }
    return s;
}
int main(){
    int v,w;
    scanf("%d %d",&v,&w);
    getchar();          
    gets(s);
    s_delete(s,v,w);
    for(int i=0;i<strlen(s)-w;i++){
        printf("%c",s[i]);
    }
}

4

Title No.: Exp05-Basic03, gjbook 3 cases - 07-01

Title: sorting two-dimensional array

Title Description: write a program to sort the input two-dimensional integer matrix incrementally.

Input: in the first row, input two positive integers n and m (n, m < = 50) corresponding to the number of rows and columns of the matrix respectively. Then enter m integers in N lines, with each integer separated by a space.

Output: sorted matrix: n lines in total, m integers in each line, separated by a Western space. There are no other characters after the last number of each line except the necessary carriage return line feed character, and there are no redundant characters at the end of the last line.

Example:

Input:
2 3
9 6 5
1 2 3
Output:
1 2 3
5 6 9
#include <stdio.h>
#include <cstring>
int a[51][51];
int b[3000];
void sort(int *a,int x){        //Bubble sort, I believe everyone knows
    for(int i=0;i<=x;i++){
        for(int j=i+1;j<=x;j++){
            if(a[j]<a[i]){
                int temp=a[i];
				a[i]=a[j];
                a[j]=temp;
			}
        }
    }
}
int main(){
    int n,m;
    scanf("%d %d",&n,&m);
    int flag=-1;
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){    //Input and save the input to a one-dimensional array
            scanf("%d",&a[i][j]);b[++flag]=a[i][j];
        }
    }
    sort(b,flag);flag=-1;        //Sort one-dimensional arrays
    for(int i=0;i<n;i++){        //Re assign this array to a one-dimensional array
        for(int j=0;j<m;j++){
            a[i][j]=b[++flag];
        }
	}
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            if(j==m-1){         //Not gorgeous output at all
                printf("%d",a[i][j]);
            }else{
			    printf("%d ",a[i][j]);
            }
        }
        if(i<n-1){
        	printf("\n");
		}
    }
}

5

Title No.: Exp05-Basic02, GJBook3-07-14

Title: classified statistical characters

Title Description:

Programming implementation: use the self-made function void count(char * str, int * upper, int *lower, int *space, int *num, int*other) to count the number of uppercase letters, lowercase letters, spaces, arrays and other characters in a given string str, and return the call point in the main program with pointer parameters and output.


Input: randomly input a string with a length of no more than 256 from the keyboard. The string may contain several white space characters.

Output: number of uppercase alphabetic characters, number of lowercase alphabetic characters, number of space characters, number of numeric characters and number of other characters. The numbers are separated by a Western space, and there are no characters after the last number.
 

Example:

Input:
123  abc   ABC
Output:
3 3 5 3 0
#include <stdio.h>
#include <cstring>
char str[258];
void count(char *str,int *upper,int *lower,int *space,int *num,int *other){
    for(int i=0;i<strlen(str);i++){        //A pile of judgments
        if(str[i]<='Z'&&str[i]>='A'){      
            (*upper)++;                    //Let's think about why we should add this bracket. Can we do without it
		}else if(str[i]<='z'&&str[i]>='a'){
		    (*lower)++; 
        }else if(str[i]==' '){
            (*space)++;
        }else if(str[i]<='9'&&str[i]>='0'){
            (*num)++;
        }else{
		    (*other)++;
        }
    }
}
int main(){
    gets(str);
    int u=0,l=0,sp=0,num=0,o=0;
    count(str,&u,&l,&sp,&num,&o);
    printf("%d %d %d %d %d",u,l,sp,num,o);
}

Tags: C data structure linked list

Posted on Sun, 21 Nov 2021 04:33:56 -0500 by Spitfire