Application of C++ Library Functions

Feiyu BLOG 2023.3.31

Information Technology Education

Python-Based Teaching

Research Topics

Academic Level Examination

Python Program Design

C++ Programming

……

Application of C++ Library FunctionsApplication of C++ Library Functions

C++ has built-in functions related to characters, which can be used for character judgment, conversion, and other operations.

int isalpha(int c): checks if c is a letter; returns a non-zero value if true, otherwise returns 0;

int isdigit(int c): checks if c is an Arabic numeral; returns a non-zero value if true, otherwise returns 0;

strlen(char* str): calculates the length of the string str;

strcat(s1, s2): concatenates two strings;

copy(s, i, i): extracts a substring. It takes a substring of length i starting from the i-th character of string s;

delete(s, i, i): deletes a substring. It removes a substring of length i starting from the i-th character of string s;

exit(int): terminates program execution;

int rand(void): generates a random integer;

srand(unsigned int): initializes the random number generator;

max(a, b): returns the larger of two numbers;

min(a, b): returns the smaller of two numbers;

【Typical Example】Xiaoming entered a string of characters on the computer; please count how many letters and how many digits there are.

Thought Analysis: One variable is responsible for counting letters, and another variable is responsible for counting digits. Use the library function isalpha() for letter counting and isdigit() for digit counting. Stop counting when a period is encountered.

Reference Code:

#include<iostream>
#include<cctype>
using namespace std;
int main(){
  char ch;
  int totalletter;
  int totalnumber;
  totalletter=0;
  totalnumber=0;
  do  {
    ch=getchar();
    if(isalpha(ch)!=0) totalletter++;
    if(isdigit(ch)!=0) totalnumber++;
  }  while(ch!='.');
  cout<<"Number of letters: "<<totalletter<<endl;
  cout<<"Number of digits: "<<totalnumber<<endl;
  return 0;
}

The isalpha function and the isdigit function are both character processing functions and must be included in the header file#include<cctype> otherwise the main program cannot call these functions.

Extension Exercises

1. Read the following program and fill in the final output result on the line below.

#include<istream>
#include<string.h>
using namespace std;
int main(){
  char p1[10]="abcdef";
  char *p2="ABCDEF";
  strcat(p1,p2+2);
  cout<<p1;
  system("pause");
}

Output: __________

2. There are 20 fifth-grade students participating in a jump rope competition. The following program uses the max function to quickly find the score of the first-place contestant. Please fill in the blanks below to make the program achieve this function.

#include<iostream>
#include<algorithm>
using namespace std;
int ans,a[20];
int main(){
  int i;
  for(i=0;i<20;i++)
    ______1_______
  ans=a[0];
  for(i=1;i<20;i++)
    ans=max(____@_____)  
  cout<<ans;
  return 0;
}

Blank 1: _________

Blank 2: _________

3. During the class party, a lottery activity will be held. All 50 students in the class have received a lottery number. Please write a program to implement the lottery activity, drawing 5 lucky students each time.

Application of C++ Library Functions1.IC026-C++ Character Array Application2.IC025-C++ Character Arrays3.IC024-C++ Two-Dimensional Array Application4.IC023-C++ Two-Dimensional Arrays5.IC022-C++ One-Dimensional Array ApplicationApplication of C++ Library Functions

Improve by1% every day, and you can achieve37 times growth in a year; if you regress by1% every day, you will weaken and even regress to zero; even a small change or a good habit can produce an amazing multiplicative effect!

Leave a Comment