2018 on C language Programming (Advanced) Jobs-3rd time jobs

Source: Internet
Author: User
Tags strcmp

Tag: Amp image appears learning address English style Apr name

Job Requirements One:

List of submitted PTA jobs:

Job Requirements II: 6-1 output Month English name

1. Design Ideas

Main description algorithm:

The first step: the need to implement a function through a string pointer, you can return the English name of a given month.

The second step: define the Shaping variable n, through the switch statement, judge each statement, if it is a numeric output month, otherwise execute default, output null

Step three: Return the main function and output as required.

2. Experiment Code:

Char*getmonth (intN) {    Switch(n) { Case 1:return "January";  Case 2:return "February";  Case 3:return "March";  Case 4:return "April";  Case 5:return " May";  Case 6:return "June";  Case 7:return "July";  Case 8:return "August";  Case 9:return "September";  Case Ten:return "October";  Case  One:return "November";  Case  A:return "December"; default:returnNULL; } } 

3. Problems encountered during commissioning and solutions

Problem 1: The pointer should be returned, not a string.

Correction method: Change printf to return

Problems encountered 2:case the numbers that follow are enclosed in single quotes.

Correction method: The case can be followed by a number, because switch is shaped after, so no single quotation marks. (Find the answer by finding your textbook on page 105)

6-2 Find Week

1. Design Ideas

Main description algorithm:

The first step: the need to use a pointer array to implement the function, according to the following table to find the week, return the corresponding ordinal.

Step Two: First define an array of pointers, save the week to the array, and use the FOR Loop statement and if condition (strcmp) to compare whether the week is the same, break back to the upper level, or return m.

Step three: Output serial number and week or-1 as required

2. Experiment Code:

intGetIndex (Char*s) {    inti,m=-1; Char*week[7]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};  for(i=0;i<7; i++)    {        if(strcmp (S, week[i]) = =0) {m=i;  Break; }      }      returnm;} 

3. Problems encountered during commissioning and solutions

Problems encountered: The topic requires that the string should be passed, so you cannot use the switch statement

Correction method: The way of the array of pointers spoken in class.

Issue 2: M=-1 is not defined in the loop so it always appears partially correct.

Correction method: First the m=-1 is defined in front, and then assign it to I, it will not appear partially correct.

6-3 calculating the longest string length

1. Design Ideas

Main description algorithm:

The first step is to implement a function that calculates the length of the longest string in a pointer array s with n elements

Step Two: First define the shape variable and the character pointer array m, judging by two for loop, * (* (s+i) +j)! = ' ", continue execution; in the second for loop, if m[i]>count, output the longest length, otherwise continue looping

Step three: The length of the longest string in the output pointer array.

2. Experiment Code:

intMax_len (Char*s[],intN) {    inti,j,m[ -]={0}; intCount=0;  for(i=0; i<n;i++)    {         for(j=0; * (* (s+i) +j)! =' /'; j + +) {} M[i]=J; }     for(i=0; i<n;i++)    {        if(m[i]>count) {Count=M[i]; }    }    returncount;} 

3. Problems encountered during commissioning and solutions

Problem encountered: pointer * (* (s+i) +j)! = ' \ "is mistaken

Correction method: It should be judged by pointers.

6-4 specifying the position output string

1. Design Ideas

Main description algorithm:

The first step: the need to write implementation function implementation of a given string and two characters, print out a given string from the position matching the first character to the second character to match the position between all characters

The second step: first define the shaping variable, through a For loop first assigns the value of the CH1 to * (s+i); Define the pointer, judge (* (S+J)!=CH2) && (* (s+j)! = ' + '), if * (s+j)! = ' + ', output, otherwise return a

Step three: Output after a large for loop, then return the value of S+i.

2. Experiment Code:

Char*match (Char*s,CharCH1,CharCH2) {    inti,j;  for(i=0; * (s+i)! =' /'; i++)    {        if(* (s+i) = =ch1) {          Char*a= &S[i];  for(J=i; (* (S+J)!=CH2) && (* (s+j)! =' /'); J + +) {printf ("%c", * (s+j)); }            if(* (s+j)! =' /') printf ("%c", * (s+j)); printf ("\ n"); returnA; }} printf ("\ n"); returnS+i;}

3. Problems encountered during commissioning and solutions

Problem encountered:%c output should be pointer * (S+I) instead of S+i

Correct method: Change S+i to * (S+i), the program runs successfully.

Requirements three, learning summary and progress:

Learning Summary:

Git's address:

Reviews classmate:

Progress Chart:

2018 on C language Programming (Advanced) Jobs-3rd time jobs

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.