Chapter 10 Experimental Strings

Chapter 10 Experimental Strings

  • Question 9.3:

Enter a line of characters and use function programming to count how many words there are. Assume that words are separated by spaces.

 

1. Problem solving ideas:

   In the main function, define a character that can input a maximum of 81 characters, printf input a string of characters, you can use gets to enter with spaces, and the num count is equal to the number of words output by calling the outer function. Outer function, if the first character in the string is not a space, mark i=1, enter the loop, while control until the last character is \0, num counts and returns the number.

 

2. Source code:

#include <stdio.h>

int CountWords(char str[]);

int main ()

{

    char  str[81];

    int num;

    printf("Input a string:\n");

    gets(str);

    num=CountWords(str);

    printf("Number of words=%d\n", num);

    return 0;

}

 

int CountWords(char str[])

{

    int  i, num;

    Surely = str [0]! = ''? 1: 0;

    i=1;

    while (str[i]!='\0')

    {

        if(str[i]!=' ' && str[i-1] == ' ')

        {

            num ++;

        }

         i++;

    }

    return num;

}

3. Screenshot of program running effect:

 

 

 

 

 

 

 

 

  • Topic 2:

Refer to Example 10.5, use character array and character pointer as function parameter to program to realize the reverse order storage of character string.

 

  1. Problem-solving ideas:

Using two character arrays, gets input characters, calls outer functions to exchange positions, puts outputs in reverse order. The outer function for loop counts the number of characters. Then, p in for starts from 0, and the other starts from len-1. The control condition p is less than the number of characters. The order is exchanged and ends with'\0'.

2. Source code:

#include <stdio.h>

#define N 80

void Inverse(char str[],char End[]);

int main ()

{

    char  str[N],End[N];

    printf("Input a string:\n");

    gets(str);

    Inverse(str,End);

    printf("The inversed string is:\n");

    puts(End);

    return 0;

}

 

void Inverse(char str[],char End[])

{

    int len ​​= 0, i, j, p;

    for (i=0;str[i]!='\0';i++)

        {

            len ++;

        }

    for(p=0,j=len-1;p<len;p++,j--)

      {

        End[p]=str[j];

      }

    End[p]='\0';

}

3. Screenshot of program running effect:

 

 

 

 

 

 

 

 

  1. , Problem-solving ideas:

The function is declared as a pointer type, outer function, pointer initialization, a char variable is defined for exchange, and the pointer after the definition is used for exchange. The pointer points to 0 until the last'\0' counts the number of strings. When Pstart comes from 0, end starts from the last one, and the number of control conditions self-added is less than the number of self-decrements, enters the loop, pointer exchange, and ends the outer function with'\0'.

 

2. Source code:

#include <stdio.h>

#include <string.h>

void Inverse(char *pStr);

int main ()

{

    char  str[80];

    printf("Input a string:\n");

    gets(str);

    Inverse(str);

    printf("The inversed string is:\n");

    puts(str);

    return 0;

}

 

void Inverse(char *pStr)

{

    int len ​​= 0;

    char temp;

    char *pStart = pStr;

    char *pEnd;

    for (; *pStart!='\0'; pStart++)

        {

            len ++;

        }

    for (pStart=pStr,pEnd=pStr+len-1; pStart<pEnd; pStart++,pEnd--)

    {

        temp = * pStart;

        * pStart = * pEnd;

 

        *pEnd = temp;

    }

}

 

3. Screenshot of program running effect:

 

 

 

 

 

 

 

  • Question 4:

  Enter the day of the week in English arbitrarily, and output the corresponding number by searching the week table as shown in the figure. If the end of the table is not found, an error message will be output

 

  1. Problem-solving ideas:

   Header file #include <string.h>, variable char is the input string, gets input, p is equal to the number of the week called from the outer layer, write the title request, judge whether p exists, enter the correct week, and return it The ordinal number, otherwise Error. Call the outer function, write the week in the two-dimensional variable, and loop until the input week has a break in the two-dimensional array, return the ordinal number, and bring it back.

 

  1. Source code:

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

int Search(char x[7]);

int main ()

{

    char x[7],p;

    printf("weekday:");

    gets(x);

    p=Search(x);

    if(p<7)

        printf("%d\n",p);

        else

        printf("Error");

    return 0;

}

int Search(char x[])

{

    int i;

    char day[7][20]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};

    for(i=0;i<7;i++)

    {

        if(strcmp(day[i],x)==0)

        break;

    }

    return i;

}

 

3. Screenshot of program running effect:

 

 

 

  • MOOC web course test results:

1. Test 1:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

6. Experimental experience and summary:

   For the study of this chapter, there are some places that I understand and some places that I don’t understand. Through reading the book again and with the help of my roommates, I have a clearer use of strings. I practice the theoretical knowledge by doing exercises and practice. Let me understand strings better.

Guess you like

Origin blog.csdn.net/weixin_48450741/article/details/112464915