当前课程知识点:C Programming >  Chapter 6 Batch Data Processing with Array >  6.7 Character Array Programming >  6.7 Character Array Programming.mp4

返回《C Programming》慕课在线视频课程列表

6.7 Character Array Programming.mp4在线视频

下一节:7.1 Function Concept and How to Define and Call Functions.mp4

返回《C Programming》慕课在线视频列表

6.7 Character Array Programming.mp4课程教案、知识点、字幕

大家好

我是云南大学信息学院丁海燕老师

欢迎走进C语言程序设计课堂

今天

我们讲解字符数组编程

下面来看例6.8

输入一行字符

统计其中有多少个单词

单词之间用空格分隔开

解题思路为

问题的关键是

怎样确定出现一个新单词了

从第1个字符开始

逐个字符进行检查

判断此字符是否是新单词的开头

如果是

就使变量num的值加1

最后得到的num的值

就是单词总数

判断是否出现新单词

可以由是否有空格出现来决定

连续的若干个空格

作为出现一次空格

一行开头的空格不统计在内

如果测出某一个字符为非空格

而它前面的字符是空格

则表示“新的单词开始了”

此时

使num累加1

如果当前字符为非空格

而其前面的字符也是非空格

则num不应再累加1

用变量word

作为判别当前

是否开始了一个新单词的标志

若word=0

表示未出现新单词

如出现了新单词

就把word置成1

前面一个字符是否空格

可以从word的值看出来

若word等于0

则表示前一个字符是空格

如果word等于1

意味着前一个字符为非空格

流程图如图所示

若当前字符=空格

成立

表示未出现新单词

使word=0

num不累加

若当前字符=空格

不成立

分两种情况

前一字符为空格

word=0

则新单词出现

使num加1

word=1

前一字符为非空格

word=1

未出现新单词

num不加1

写成程序为

if(c==' ')

word=0

else if(word==0)

{ word=1;

num++; }

输入“I am a boy.”

有关参数状态如图所示

程序如下

char string[81],c;

定义数组string

和c

int i,num=0,word=0;

注意num,word

一定要设初始值

gets(string);

输入字符串

for (i=0;

(c=string[i])!=‘\0’;i++)

if(c==‘ ’) word=0;

else if(word==0)

{ word=1;

num++; }

最后输出num的值

(c=string[i])!=‘\0’

相当于c=string[i];

c!=‘\0’;

程序运行输入I am a boy.

则输出4 words

例6.8

输入一行字符

统计其中有多少个单词

单词之间用空格分隔开

输入以下程序

定义字符数组string

字符变量c

定义变量i

num用来对单词计数

word表示是否出现单词

用 gets 输入字符串

对每一个字符进行判断

若字符为空格

说明没有出现单词

若字符非空

且之前还未出现单词

则单词出现

word=1

对单词计数

num +1

输出单词个数

编译

连接

运行

输入“I am a boy.”

输出

4 words

下面来看例6.9

有3个字符串

要求找出其中最大者

解题思路为

设一个二维的字符数组str

大小为3×10

每一行存放一个字符串

定义char str[3][10];

可以把str[0]

str[1]

str[2]

看作3个一维数组

可以把它们如同一维数组那样

进行处理

用循环语句

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

gets (str[i]);

输入China, Japan, India三个字符串

str1

str2

str3

存储状态如图所示

c,h,i,n,a

五个斜线0

J, a, p ,a ,n

五个斜线0

I,n,d,i,a

五个斜线0

利用字符串比较函数strcmp

经过三次两两比较

就可得到最大值

把它放在一维字符数组string中

程序如下

if (strcmp(str[0],str[1])>0)

strcpy(string,str[0]); else

strcpy(string,str[1]);

if (strcmp(str[2],string)>0)

strcpy(string,str[2]);

经过第1 个if语句的处理

string中存放了str[0]

和str[1]中的“大者”

第2个if语句

把string和str[2]比较

把大者放在string中

最后在string中的

就是str[0]

str[1]

str[2]三者中的最大值

程序如下

{char str[3][10]; char string[10]; int i;

定义二维字符数组

一维字符数组

和整型变量i

for (i=0;i<3;i++) gets (str[i]);

输入3个字符串

if (strcmp(str[0],str[1])>0)

strcpy(string,str[0]); else

strcpy(string,str[1]);

这是将str[0]

str[1]中较大者放在string中

if (strcmp(str[2],string)>0)

strcpy(string,str[2]);

这是将str[0] ,str[1]

str[2]中最大者放在string中

最后用printf输出string中的字符串

输入China, Japan, India三个字符串

输出最大值为Japan.

下面来看例6.10

字符串的排序

问题描述如下

输入三个人的姓名

将姓名按由小到大的顺序排列

问题分析

定义一个3×20的二维字符数组name

存储三个人的名字

通过gets 函数

分别读入3 个人名字符串

用冒泡法实现排序

用strcmp 函数进行两两比较

用strcpy为字符数组赋值

用3×20的二维字符数组name

存储三个人的名字

char name[3][20];

二维数组存储状态如图所示

可以把name看作是有三个元素

name[0],name[1]

name[2]的一维数组

name[0]

name[1]

name[2]

分别为三个一维字符数组的名字

程序如下

#include

#include

#define M 3

#define N 20

int函数中

{ char name[M][N], str[20];

定义二维数组name

一维数组str

int i,j;

printf("input people's name: ");

for(i=0;i

(name[i]);

输入三个人的姓名

用冒泡法对三个字符串按升序排序

M个字符串排序

共需要比较M-1趟

在第i趟比较中

要进行M-i次两两比较

每次用strcmp 函数

完成相邻字符串的比较

若后者比前者小

则借助字符数组str

用strcpy

交换两个字符数组中的字符串

程序如下for

(i=1;i

for(j=0;j

if

(strcmp

(name[j],name[j+1])>0)

{ strcpy(str,name[j]);

strcpy(name[j]

,name[j+1]);

strcpy(name[j+1],str); }

最后输出排序的字符串

for(i=1;i

puts(name[i]);

程序运行时

若输入三个姓名

Li ming

Wei fen

Ding li

升序排序为Ding li

Li ming

Wei fen

好了

同学们

字符数组编程

我们就学到这儿

下节课再见

下节课再见!

C Programming课程列表:

Chapter 1 Introduction

-1.1 The development and characteristics of C language

--1.The development and characteristics of C language .mp4

--1.1 Self test questions

-1.2 A simple C Language program

--1.2 A simple C Language program.mp4

--Discussion unit

--【Source program】 Example 1.1 output a line of text Hello, world! "

--【Source program】 Example 1.2 program composed of multiple functions, find the larger of two integers

--1.2 Self test questions

-1.3 Program, Programming Language and C Program Running Steps

--1.3 Program, Programming Language and C Program Running Steps.mp4

--Discussion unit

--Running steps of C source program in CodeBlocks

--1.3 Self test questions

-Course references

-chapter 1 Self-Test

Chapter 2 Algorithm

-2.1 The Concept and Description of Algorithms

--2.1 The Concept and Description of Algorithms.mp4

--Discussion unit

--2.1 Self test questions

-2.2 Examples of Simple Algorithms, Computational Thinking and Structured Programming

--2.2 Examples of Simple Algorithms, Computational Thinking and Structured Programming.mp4

--Discussion unit

--2.2 Self test questions

-Chapter 2 Self test questions

Chapter 3 Programming in C

-3.1 Simple Structure and Identifier of C Language Program

--3.1 Simple Structure and Identifier of C Language Program.mp4

--【Source program】 Example 3.1 input two integers and output the sum of two numbers.

--【Source program】 Example 3.2 input two integers and output the average value.

--【Source program】 Example 3.3 output the value of the character variable.

--3.1 Self test questions

-3.2 Constants, Variables and Assignments

--3.2 Constants, Variables and Assignments.mp4

--Discussion unit

--3.2 Self test questions

-3.3 Arithmetic, assignment, increment and decrement operators

--3.3 Arithmetic, assignment, increment and decrement operators.mp4

--3.3 Self test questions

-3.4 Conditions, commas, addresses, byte operators, and mixed operations among various numerical data

--3.4 Conditions, commas, addresses, byte operators, and mixed operations among various numerical data

--【Source program】 example of sizeof

--3.4 Self test questions

-3.5 Input and Output Examples and Character Input and Output

--3.5 Input and Output Examples and Character Input and Output.mp4

--【Source program】 Example. Find the root of quadratic equation of one variable.

--【Source program】 Example 1. Successively output three characters of BOY.

--【Source program】Example 2. Input 3 characters of BOY from the keyboard and output them to the screen

--3.5 Self test questions

-3.6 Formatted output printf function

--3.6 Formatted output printf function.mp4

--【Source program】 Example 3.4 output of integer data.

--【Source program】 Example 3.5 real data output.

--【Source program】 Example 3.6 character data output.

--【Source program】 Example 3.7 uses %s to output a string.

--3.6 Self test questions

-3.7 Formatted input scanf function

--3.7 Formatted input scanf function.mp4

--Discussion unit

--【Source program】 Example 3.8 Input and output integer data

--【Source program】Example 3.9 Input and output single precision and double precision real data.

--【Source program】Example 3.10 input and output character data.

--【Source program】Example 3.11 input and output string.

--3.7 Self test questions

-3.8 Basic Data Types of C Language

--3.8 Basic Data Types of C Language.mp4

--3.8 Self test questions

-Chapter 3 Self test questions

-Operator and expression self test questions

Chapter 4 Selection Structure

-4.1 Relational operators, logical operators, and if statements

--4.1 Relational operators, logical operators, and if statements.mp4

--Discussion unit

--【source example】Example 4.1 Find the root of the quadratic equation of one variable.

--【Source program】Example 4.2 Input two real numbers and output them from small to large.

--4.1 Self test questions

-4.2 Switch Statement

--4.2 Switch Statement .mp4

--Discussion unit

--【Source program】Example 4.3 uses switch statement to implement simple menu program

--【Source program】Example 4.4 converts centesimal score into the corresponding grade system score.

--4.2 Self test questions

-4.3 Examples of Selection Structural Programs

--4.3 Examples of Selection Structural Programs.mp4

--【Source program】 Example 4.5 determines whether a year is a leap year.

--【Source program】 Example 4.6 find the solution of quadratic equation of one variable.

--【Source program】Example 4.7 calculate the transportation cost for the user.

--4.3 Self test questions

-Chapter 4 Self test questions

Chapter 5 Loop Structure

-5.1 While and Do-While Statement

--5.1 While and Do…while statement.mp4

--Discussion unit

--【Source program】example 5.1 Use while statement to find 1 + 2 + 3 + +100

--【Source program】example 5.2 using do while statement to find 1 + 2 + 3 + +100

--5.1 Self test questions

-5.2 For Statement

--5.2 for statement.mp4

--【Source program】example 5.3 Use for loop to find 1 + 2 + 3 +100

--5.2 Self test questions

-5.3 Change the State of Loop Execution and Nested Loop

--5.3 Change the state of loop execution and nested loop .mp4

--【Source program】example 5.4 collecting charity donations

--【Source program】example 5.5 output a number between 100 and 200 that cannot be divided by 3.

--【Source program】multiplication table

--【Source program】example 5.6 Output the following 4*5 matrix.

--5.3 Self test questions

-5.4 Loop Structure Program example 1

--5.4 Loop structure program example 1.mp4

--【Source program】example 1

--【Source program】example 2

--【Source program】example 3

--【Source program】 Example 4

--5.4 Self test questions

-5.5 Loop Structure Program example 2

--5.5 Loop structure program example 2.mp4

--【Source program】example 5. Output the following figure.

--【Source program】example6. The problem of 100 chickens and 100 coins

--【Source program】example 7. Find the approximate value of PI

--5.5 Self test questions

-Chapter 5 Self test questions

Chapter 6 Batch Data Processing with Array

-6.1 Definition, Reference and Initialization of One-Dimensional Arrays

--6.1 Definition, Reference of One-Dimensional Arrays.mp4

--Discussion unit

--6.1 Self test questions

-6.2 One-Dimensional Array Programming

--6.2 One-Dimensional Array Programming.mp4

--【Source program】example 1. Fibonacci sequence

--【Source program】example 2. Find the maximum value and the minimum value.

--【Source program】example 3 exchanging array elements in reverse order.

--【Source program】example 4. Bubble sorting.

--6.2 Self test questions

-6.3 Definition, Reference and Initialization of Two-Dimensional Arrays

--6.3 Definition, Reference of Two-Dimensional Arrays.mp4

--6.3 Self test questions

-6.4 Two-Dimensional Array Programming

--6.4 Two-Dimensional Array Programming.mp4

--【Source program】example 1. Get the average score of each subject.

--【Source program】Example 2. The elements of row and column of a 2D array are interchanged

--6.4 Self test questions

-6.5 Definition, initialization and input and output of character arrays

--6.5 Definition, initialization and input and output of character arrays .mp4

--Discussion unit

--【Source program】Example 6.6 Output a known string.

--【Source program】Example 6.7 Output a diamond.

--【Source program】Example 6.10 Sorting of strings.

--6.5 Self test questions

-6.6 String Processing Function

--6.6 String Processing Function.mp4

--6.6 Self test questions

-6.7 Character Array Programming

--6.7 Character Array Programming.mp4

--6.7 Self test questions

-Chapter 6 Self test questions

Chapter 7 Modular Programming with Functions

-7.1 Function Concept and How to Define and Call Functions

--7.1 Function Concept and How to Define and Call Functions.mp4

--【Source program】Example 7.1

--7.1 Self test questions

-7.2 Data Transfer in Function Call, Call Procedure and Function Return Value

--7.2 Data Transfer in Function Call, Call Procedure and Function Return Value.mp4

--Discussion unit

--【Source program】Example 7.2

--7.2 Self test questions

-7.3 Declarations of called functions and nested calls to functions

--7.3 Declarations of called functions and nested calls to functions .mp4

--【Source program】Example 7.4 use a function to find the sum of t

--7.3 Self test questions

-7.4 Recursive call to function

--7.4 Recursive call to function.mp4

--【Source program】Example 7.6. How old is the fifth student?

--【Source program】Example 7.7 Use recursion method to find n!

--7.4 Self test questions

-7.5 Array as function parameter (1)

--7.5 Array as function parameter (1).mp4

--Discussion unit

--【Source program】Example 7.10

--7.5 Self test questions

-7.6 Array as function parameter (2)

--7.6 Array as function parameter (2).mp4

--【Source program】Selection sorting.

--【Source program】Example 7.13 find the maximum of a 3×4 matrix.

--7.6 Self test questions

-7.7 Local and global variables, internal and external functions

--7.7 Local and global variables, internal and external functions.mp4

--【Source program】Example 7.14

--7.7 Self test questions

-7.8 The Survival Period of Variables and the Storage Mode of Local Variables

--7.8 The Survival Period of Variables and the Storage Mode of Local Variables.mp4

--【Source program】Example 7.17

--7.8 Self test questions

-7.9 Storage Categories of Global Variables

--7.9 Storage Categories of Global Variables.mp4

--7.9 Self test questions

-Chapter 7 Self test questions

Chapter 8 Pointer

-8.1 Pointer Concept, Definition and Reference of Pointer Variables

--8.1 Pointer Concept, Definition and Reference of Pointer Variables.mp4

--【Source program】Example 8.1

--8.1 Self test questions

--Discussion unit

-8.2 Pointer variables as function parameters

--8.2 Pointer variables as function parameters.mp4

--Discussion unit

--【Source program】Example 8.3. Exchange two data with function call.

--8.2 Self test questions

-8.3 Pointer of array element, operation of pointer and reference of array element by pointer

--8.3 Pointer of array element, operation of pointer and reference of array element by pointer.mp4

--【Source program】Point to array elements with pointer variables.

--8.3 Self test questions

-8.4 Using Array Name as Function Parameter

--8.4 Using Array Name as Function Parameter.mp4

--【Source program】Store array elements in reverse order.

--8.4 Self test questions

-8.5 Reference to multidimensional arrays by pointers

--8.5 Reference to multidimensional arrays by pointers.mp4

--【Source program】Pointer variable pointing to one-dimensional array.

--8.5 Self test questions

-8.6 Referencing strings through pointers

--8.6 Referencing strings through pointers.mp4

--【Source program】Reference string by pointer.

--8.6 Self test questions

-8.7 Character pointer as function parameter

--8.7 Character pointer as function parameter.mp4

--【Source program】Character pointer as function parameter.

--8.7 Self test questions

-8.8 Pointer pointing to function

--8.8 Pointer pointing to function.mp4

--【Source program】Pointer variable pointing to the function.

--8.8 Self test questions

-8.9 Functions that return pointer values

--8.9 Functions that return pointer values.mp4

--【Source program】Intercept substring.

--8.9 Self test questions

-8.10 Pointer arrays and multiple pointers

--8.10 Pointer arrays and multiple pointers.mp4

--【Source program】Sorting of strings.

--8.10 Self test questions

-8.11 Dynamic memory allocation and pointer variables pointing to it

--8.11 Dynamic memory allocation and pointer variables pointing to it.mp4

--Discussion unit

--【Source program】Dynamic memory allocation.

--8.11 Self test questions

-Chapter 8 Self test questions

Chapter 9 Structure

-9.1 Define and use structural variables

--9.1 Define and use structural variables.mp4

--【Source program】Structure variable

--9.1 Self test questions

-9.2 Using structure arrays

--9.2 Using structure arrays.mp4

--Discussion unit

--【Source program】Structure array

--9.2 Self test questions

-9.3 Structure pointer

--9.3 Structure pointer.mp4

--【Source program】Structure pointer

--9.3 Self test questions

-Chapter 9 Self test questions

CodeBlocks Baidu online disk download

-CodeBlocks Baidu online disk download address

Final Exam

-Final Exam

--final exam

6.7 Character Array Programming.mp4笔记与讨论

也许你还感兴趣的课程:

© 柠檬大学-慕课导航 课程版权归原始院校所有,
本网站仅通过互联网进行慕课课程索引,不提供在线课程学习和视频,请同学们点击报名到课程提供网站进行学习。