韵动代码: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 
其他字符数组:@ 
相关文章
|
8天前
|
存储 搜索推荐 Serverless
【C++航海王:追寻罗杰的编程之路】哈希的应用——位图 | 布隆过滤器
【C++航海王:追寻罗杰的编程之路】哈希的应用——位图 | 布隆过滤器
18 1
|
2天前
|
JSON Android开发 C++
Android c++ core guideline checker 应用
Android c++ core guideline checker 应用
|
27天前
|
C++ 索引 运维
开发与运维数组问题之在C++中数组名和指针是等价如何解决
开发与运维数组问题之在C++中数组名和指针是等价如何解决
18 6
|
27天前
|
存储 安全 C++
开发与运维数组问题之声明一个数组如何解决
开发与运维数组问题之声明一个数组如何解决
33 6
|
27天前
|
存储 C++ 容器
开发与运维数组问题之C++标准库中提供数据容器作为数组的替代如何解决
开发与运维数组问题之C++标准库中提供数据容器作为数组的替代如何解决
37 5
|
4天前
|
安全 编译器 C语言
|
27天前
|
前端开发 编译器 程序员
协程问题之为什么 C++20 的协程代码比其他语言的协程 demo 长很多如何解决
协程问题之为什么 C++20 的协程代码比其他语言的协程 demo 长很多如何解决
|
29天前
|
算法 NoSQL 编译器
如何编写可维护的C++代码
如何编写可维护的C++代码
|
1月前
|
vr&ar C++
1695. 删除子数组的最大得分(C++,滑动窗口)
1695. 删除子数组的最大得分(C++,滑动窗口)
|
6天前
|
C++ 容器
C++中自定义结构体或类作为关联容器的键
C++中自定义结构体或类作为关联容器的键
13 0