鱼C论坛

 找回密码
 立即注册
查看: 2044|回复: 2

【c++问题求助】编写一个提取子字符串的函数

[复制链接]
发表于 2022-6-6 21:02:54 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
(1)编写一个提取子字符串的函数,函数原型为 char *substr(char *s,int start,int end); *s为源字符串,start为开始位置,end为结束位置。
(2)编写函数int find(int *data,int n,int x);其功能是在data所指向的一维数组中查找值为x的元素,若找到,则函数返回该元素的下标;若找不到,则函数返回-1.其中n指定数组元素个数。编写完整的程序并测试。
求助一下大佬们
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-6-6 23:10:58 | 显示全部楼层
你确定是char *,而不是string吗?
这可是C++,你有什么理由使用char *类型?

  1. $ ls
  2. main.cpp
  3. $ cat main.cpp
  4. #include <iostream>
  5. #include <string>

  6. using std::string;
  7. using std::cout, std::endl;

  8. // [start, end)
  9. const string substr(const string &s, size_t start, size_t end) {
  10.     if(start > s.size()) start = s.size();
  11.     if(end > s.size()) end = s.size();
  12.     if(end <= start) return "";
  13. #if 0
  14.     return s.substr(start, end - start);
  15. #else
  16.     string result;
  17.     for(size_t i = start; i < end; ++i) {
  18.         result += s[i];
  19.     }
  20.     return result;
  21. #endif
  22. }

  23. int find(int *data, size_t size, int x) {
  24.     for(size_t i = 0; i < size; ++i) {
  25.         if(data[i] == x) return i;
  26.     }
  27.     return -1;
  28. }

  29. int main() {
  30.     cout << substr("hello world!", 3, 8) << endl;
  31.     cout << substr("hello world!", 3, 99) << endl;
  32.     cout << substr("hello world!", 99, 3) << endl;
  33.     cout << substr("hello world!", 99, 99) << endl;
  34.     cout << substr("hello world!", 8, 3) << endl;
  35.     cout << substr("hello world!", 0, 11) << endl;
  36.     cout << substr("hello world!", 0, 12) << endl;
  37.     cout << substr("hello world!", 1, 11) << endl;
  38.     cout << substr("hello world!", 1, 12) << endl;

  39.     int data[] = {1, 9, 2, 3, 8, 6, 7, 5, 4};
  40.     const size_t data_size = sizeof(data) / sizeof(data[0]);
  41.     cout << find(data, data_size, 0) << endl;
  42.     cout << find(data, data_size, -1) << endl;
  43.     cout << find(data, data_size, -99) << endl;
  44.     cout << find(data, data_size, 99) << endl;
  45.     cout << find(data, data_size, 10) << endl;
  46.     cout << find(data, data_size, 9) << endl;
  47.     cout << find(data, data_size, 1) << endl;
  48.     cout << find(data, data_size, 5) << endl;
  49.     cout << find(data, data_size, 7) << endl;
  50.     return 0;
  51. }
  52. $ g++-debug -o main main.cpp
  53. $ ./main
  54. lo wo
  55. lo world!



  56. hello world
  57. hello world!
  58. ello world
  59. ello world!
  60. -1
  61. -1
  62. -1
  63. -1
  64. -1
  65. 1
  66. 0
  67. 7
  68. 6
  69. $
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-6-6 23:27:58 | 显示全部楼层
人造人 发表于 2022-6-6 23:10
你确定是char *,而不是string吗?
这可是C++,你有什么理由使用char *类型?

一道c++的题目,不用string我也很头痛
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-24 05:10

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表