韵动代码:C++数组实践与应用之路2

简介: 韵动代码:C++数组实践与应用之路

4.3 字符数组

字符数组是一种特殊的数组,其中的元素是字符。它可以用于存储和处理字符串。


4.3.1 字符数组的定义

在C++中,字符数组是一种特殊类型的数组,用于存储字符串。字符数组可以用来存储字符序列,以及用于处理字符串相关的操作。


示例代码:

#include <iostream>
using namespace std;
int main() 
{
    char name[10];
    cout << "请输入您的名字:";
    cin >> name;
    cout << "您好," << name << "!" << endl;
    return 0;
}

程序运行结果如下

请输入您的名字:肯德德兄弟麦当当
您好,肯德德兄弟麦当当!

4.3.2 字符数组的初始化

字符数组可以通过多种方式进行初始化,包括直接赋值、使用字符串常量等。


示例代码:

#include <iostream>
using namespace std;
int main() 
{
    char greeting[6] = "Hello";
    cout << "Greeting message: " << greeting << endl;
    return 0;
}

程序运行结果如下

Greeting message: Hello

4.3.3 字符数组的引用

通过下标可以访问和修改字符数组中的单个字符。字符数组的引用可以用于获取字符数组中特定位置的字符,并进行相关操作。


【例4-11】字符数组的引用:

#include <iostream>
using namespace std;
int main() 
{
   char str[6] = "Hello";
   cout << "第一个字符:" << str[0] << endl;
   cout << "最后一个字符:" << str[4] << endl;
   return 0;
}

程序运行结果如下

第一个字符:H
最后一个字符:O

【利4-12】输出一个菱形图形

#include <iostream>
using namespace std;
int main() 
{
   char c = '*';
   for(int i = 1; i <= 5; i++) 
   {
      for(int j = 1; j <= 5 - i; j++) 
      {
         cout << " ";
      }
      for(int k = 1; k <= 2 * i - 1; k++) 
      {
         cout << c;
      } 
      cout << endl;
   }
   for(int i = 4; i >= 1; i--) 
   {
      for(int j = 1; j <= 5 - i; j++)
      {
         cout << " ";
      }
      for(int k = 1; k <= 2 * i - 1; k++) 
      {
         cout << c;
      } 
      cout << endl;
   }
   return 0;
}

程序运行结果如下

     *
    ***
   *****
  *******
 *********
  *******
   *****
    ***
     *

【到4-13】字符数组整体引用:

#include <iostream>
using namespace std;
int main() 
{
    char str[6] = "Hello";
    char *ptr = str;
    cout << "字符串为:" << ptr << endl;
    return 0;
}

程序运行结果如下

字符串为:Hello

4.3.4 字符串与字符串结束标志

字符串是由字符组成的字符数组,以空字符 ‘\0’ 结尾。可以通过字符串结束标志来判断字符串的结束位置。


【到4-14】求一个字符串的实际长度:

#include <iostream>
using namespace std;
int main() 
{
    char str[] = "Hello";
    int length = 0;
    while (str[length] != '\0') 
    {
        length++;
    }
    cout << "字符串的长度为:" << length << endl;
    return 0;
}

程序运行结果如下

字符串的长度为:5

4.4 常用的字符串处理函数

4.4.1 stremp()函数

【例 4-15】stremp()函数的应用


stremp()函数用于比较两个字符串是否相同。


示例代码:

#include<iostream>
#include<cstring>
using namespace std;
int main() 
{
    char str1[] = "Hello";
    char str2[] = "Hello";  
    int result = strcmp(str1, str2);   
    if (result == 0) 
    {
        cout << "字符串相同" << endl;
    } 
    else 
    {
        cout << "字符串不相同" << endl;
    }    
    return 0;
}

输出:

字符串相同

4.4.2 strepy()函数

【例4-16】strepy函数的应用


strepy()函数用于比较两个字符串是否相同,忽略大小写。


示例代码:

#include<iostream>
#include<cstring>
using namespace std;
int main() 
{
    char str1[] = "Hello";
    char str2[] = "hello";    
    int result = stricmp(str1, str2);    
    if (result == 0) 
    {
        cout << "字符串相同" << endl;
    } 
    else 
    {
        cout << "字符串不相同" << endl;
    }    
    return 0;
}

输出:

字符串相同

4.4.3 strcat()函数

【例4-17】strcat函数的应用


strcat()函数用于将一个字符串追加到另一个字符串的末尾。


示例代码:

#include<iostream>
#include<cstring>
using namespace std;
int main() 
{
    char str1[20] = "Hello";
    char str2[] = " World";   
    strcat(str1, str2);    
    cout << "合并后的字符串为:" << str1 << endl;    
    return 0;
}

输出:

合并后的字符串为:Hello World

4.4.4 strlen()函数

【例4-18】strlen() 函数的应用


strlen()函数用于获取字符串的长度。


示例代码:

#include<iostream>
#include<cstring>
using namespace std;
int main() 
{
    char str[] = "Hello";
    int length = strlen(str);    
    cout << "字符串的长度为:" << length << endl;   
    return 0;
}

输出:

字符串的长度为:5

4.4.5 strlwr()函数

strlwr()函数用于将字符串转换为小写。


示例代码:

#include<iostream>
#include<cstring>
using namespace std;
int main() 
{
    char str[] = "Hello";
    strlwr(str);    
    cout << "转换后的字符串为:" << str << endl;    
    return 0;
}

输出:

转换后的字符串为:hello

4.4.6 strlwr()函数

strupr()函数用于将字符串转换为大写。


示例代码:

#include<iostream>
#include<cstring>
using namespace std;
int main() 
{
    char str[] = "hello";
    strupr(str);    
    cout << "转换后的字符串为:" << str << endl;  
    return 0;
}

输出:

转换后的字符串为:HELLO

4.4.7 字符数组应用实例

【例4-19】字符串翻转


示例代码:

#include<iostream>
#include<cstring>
using namespace std;
void reverseString(char str[]) 
{
    int length = strlen(str);    
    for (int i = 0; i < length / 2; i++) 
    {
        char temp = str[i];
        str[i] = str[length - i - 1];
        str[length - i - 1] = temp;
    }
}
int main() 
{
    char str[] = "Hello World";    
    reverseString(str);    
    cout << "翻转后的字符串为:" << str << endl;   
    return 0;
}

输出:

翻转后的字符串为:dlroW olleH

【例4-20】判断字符串是否为回文


示例代码:

#include<iostream>
#include<cstring>
using namespace std;
bool isPalindrome(char str[]) 
{
    int length = strlen(str);    
    for (int i = 0; i < length / 2; i++) 
    {
        if (str[i] != str[length - i - 1]) 
        {
            return false;
        }
    }
    return true;
}
int main() 
{
    char str[] = "madam";   
    if (isPalindrome(str)) 
    {
        cout << "是回文字符串" << endl;
    } 
    else 
    {
        cout << "不是回文字符串" << endl;
    }    
    return 0;
}

输出:

是回文字符串

C++学习笔记(综合实例)


4.5 综合实例

4.5.1 折半查找法

【例4-21】折半查找法的应用


折半查找法是一种高效的查找算法,适用于有序数组。它的基本思想是通过比较中间元素和查找目标的大小关系,将查找范围不断缩小一半,直到找到目标或查找范围为空。


示例代码:

#include<iostream>
using namespace std;
int binarySearch(int arr[], int target, int left, int right) 
{
    while (left <= right) 
    {
        int mid = (left + right) / 2;        
        if (arr[mid] == target) 
        {
            return mid;
        }
         else if (arr[mid] < target) 
        {
            left = mid + 1;
        } 
        else 
        {
            right = mid - 1;
        }
    }    
    return -1;
}
int main() 
{
    int arr[] = {1, 3, 5, 7, 9, 11, 13, 15};
    int target = 9;
    int size = sizeof(arr) / sizeof(arr[0]);    
    int result = binarySearch(arr, target, 0, size - 1);    
    if (result != -1) 
    {
        cout << "找到目标,索引为:" << result << endl;
    } 
    else 
    {
        cout << "未找到目标" << endl;
    }    
    return 0;
}

输出:

找到目标,索引为:4

4.5.2 将字符数组中的字符分类

【例4-22】将字符数组中的字符分类的应用


将一个字符数组中的字符按照字母、数字和其他字符分别存放到三个不同的数组中。


示例代码:

#include<iostream>
using namespace std;
void classifyCharacters(char str[], char letters[], char digits[], char others[], int& letterSize, int& digitSize, int& otherSize) 
{
    letterSize = 0;
    digitSize = 0;
    otherSize = 0;    
    for (int i = 0; str[i] != '\0'; i++) 
    {
        if (isalpha(str[i])) 
        {
            letters[letterSize++] = str[i];
        } 
        else if (isdigit(str[i])) 
        {
            digits[digitSize++] = str[i];
        }
        else 
        {
            others[otherSize++] = str[i];
        }
    }    
    letters[letterSize] = '\0';
    digits[digitSize] = '\0';
    others[otherSize] = '\0';
}
int main()
{
    char str[] = "Hello 123@";
    char letters[100], digits[100], others[100];
    int letterSize, digitSize, otherSize;    
    classifyCharacters(str, letters, digits, others, letterSize, digitSize, otherSize);   
    cout << "字母数组:";
    for (int i = 0; i < letterSize; i++) 
    {
        cout << letters[i] << " ";
    }
    cout << endl;    
    cout << "数字数组:";
    for (int i = 0; i < digitSize; i++) 
    {
        cout << digits[i] << " ";
    }
    cout << endl;    
    cout << "其他字符数组:";
    for (int i = 0; i < otherSize; i++) 
    {
        cout << others[i] << " ";
    }
    cout << endl;    
    return 0;
}

输出:

字母数组:H e l l o 
数字数组:1 2 3 
其他字符数组:@ 
相关文章
|
4天前
|
存储 安全 算法
【Linux | C++ 】基于环形队列的多生产者多消费者模型(Linux系统下C++ 代码模拟实现)
【Linux | C++ 】基于环形队列的多生产者多消费者模型(Linux系统下C++ 代码模拟实现)
21 0
|
4天前
|
算法 Linux 数据安全/隐私保护
【Linux | C++ 】生产者消费者模型(Linux系统下C++ 代码模拟实现)
【Linux | C++ 】生产者消费者模型(Linux系统下C++ 代码模拟实现)
9 0
|
10天前
|
C++
【C++】一文深入浅出带你参透库中的几种 [ 智能指针 ]及其背后实现原理(代码&图示)
【C++】一文深入浅出带你参透库中的几种 [ 智能指针 ]及其背后实现原理(代码&图示)
|
10天前
|
C++ 数据格式
【C++】C++中的【文件IO流】使用指南 [手把手代码演示] & [小白秒懂]
【C++】C++中的【文件IO流】使用指南 [手把手代码演示] & [小白秒懂]
【C++】C++中的【文件IO流】使用指南 [手把手代码演示] & [小白秒懂]
|
10天前
|
编译器 C++
【C++】【C++的常变量取地址问题(对比C的不同)】const修饰的常变量&volatile修饰用法详解(代码演示)
【C++】【C++的常变量取地址问题(对比C的不同)】const修饰的常变量&volatile修饰用法详解(代码演示)
|
11天前
|
C++
【期末不挂科-C++考前速过系列P6】大二C++实验作业-模板(4道代码题)【解析,注释】
【期末不挂科-C++考前速过系列P6】大二C++实验作业-模板(4道代码题)【解析,注释】
【期末不挂科-C++考前速过系列P6】大二C++实验作业-模板(4道代码题)【解析,注释】
|
11天前
|
Serverless C++ 容器
【期末不挂科-C++考前速过系列P5】大二C++实验作业-多态性(3道代码题)【解析,注释】
【期末不挂科-C++考前速过系列P5】大二C++实验作业-多态性(3道代码题)【解析,注释】
|
4天前
|
设计模式 安全 算法
【C++入门到精通】特殊类的设计 | 单例模式 [ C++入门 ]
【C++入门到精通】特殊类的设计 | 单例模式 [ C++入门 ]
14 0
|
5天前
|
C语言 C++
【C++】string类(常用接口)
【C++】string类(常用接口)
13 1
|
2天前
|
编译器 C++
【C++】继续学习 string类 吧
首先不得不说的是由于历史原因,string的接口多达130多个,简直冗杂… 所以学习过程中,我们只需要选取常用的,好用的来进行使用即可(有种垃圾堆里翻美食的感觉)
7 1