鱼C论坛

 找回密码
 立即注册
查看: 1943|回复: 15

[已解决]小白请教一道C语言编程题

[复制链接]
发表于 2020-4-28 20:42:55 | 显示全部楼层 |阅读模式
5鱼币
请编写一个程序,用一个单词替代给定字符串中的单词。在文本中使用”It is good to program in PASCAL language“ 中,用“C”将”PASCAL“替换掉。
(小白表示毫无思绪,望技术大牛多多指教!)
最佳答案
2020-4-28 20:42:56
sulley 发表于 2020-4-28 22:15
void replace(char*, char*, char*) ”:无法将参数 264Project 7源.cpp从”const char[7] ”转换为”c ...
  1. #include <stdio.h>
  2. #include <string.h>

  3. #pragma warning( disable : 4996)
  4. void replace(char *str, char *s, char *t)
  5. {
  6.         char temp[256], *p;
  7.         int lens = strlen(s),
  8.                 lent = strlen(t);

  9.         p = strstr(str, s);
  10.         strcpy(temp, p + lens);
  11.         strcpy(p, t);
  12.         strcpy(p + lent, temp);
  13.         }

  14.         int main()
  15.         {
  16.                 char s[256] = "It is good to program in PASCAL language.";
  17.                 char * ss = "PASCAL";
  18.                 char * t = "C";

  19.                 replace(s, ss, t);
  20.                 puts(s);
  21.                 while (1);
  22.         }
复制代码


可以这样修改,应该是你的机器对于类型的检查比较严格。

最佳答案

查看完整内容

可以这样修改,应该是你的机器对于类型的检查比较严格。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-4-28 20:42:56 | 显示全部楼层    本楼为最佳答案   
sulley 发表于 2020-4-28 22:15
void replace(char*, char*, char*) ”:无法将参数 264Project 7源.cpp从”const char[7] ”转换为”c ...
  1. #include <stdio.h>
  2. #include <string.h>

  3. #pragma warning( disable : 4996)
  4. void replace(char *str, char *s, char *t)
  5. {
  6.         char temp[256], *p;
  7.         int lens = strlen(s),
  8.                 lent = strlen(t);

  9.         p = strstr(str, s);
  10.         strcpy(temp, p + lens);
  11.         strcpy(p, t);
  12.         strcpy(p + lent, temp);
  13.         }

  14.         int main()
  15.         {
  16.                 char s[256] = "It is good to program in PASCAL language.";
  17.                 char * ss = "PASCAL";
  18.                 char * t = "C";

  19.                 replace(s, ss, t);
  20.                 puts(s);
  21.                 while (1);
  22.         }
复制代码


可以这样修改,应该是你的机器对于类型的检查比较严格。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-4-28 21:22:08 | 显示全部楼层
这个其实就是字符串匹配的问题,匹配到原字符串中指定子串,问题也就解决了,可以参考下算法教材上的KMP算法或者BM算法之类
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-4-28 21:39:45 | 显示全部楼层
ly落叶 发表于 2020-4-28 21:22
这个其实就是字符串匹配的问题,匹配到原字符串中指定子串,问题也就解决了,可以参考下算法教材上的KMP算 ...

跪求大佬再指点指点,我还没有学到KMP和BM。刚才去搜索也是看得一头雾水。
恕我愚钝,我还是不知道怎么敲代码2333
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-4-28 21:46:03 | 显示全部楼层
C语言的话用指针变量存储该字符串,然后用while语句对指针地址自加进行解引用,检测到当前地址内容为‘\0’或‘\n’即退出循环,然后在循环中进行判断,记录首字母和末字母的地址,然后将该地址的值进行替换即可
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-4-28 21:47:32 | 显示全部楼层
python的话可以用字符串的内置函数replace()直接进行替换
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-4-28 21:54:12 | 显示全部楼层
Qdb 发表于 2020-4-28 21:47
python的话可以用字符串的内置函数replace()直接进行替换

戳中痛点!奈何是要使用C语言,暴风哭泣
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-4-28 21:58:11 | 显示全部楼层
Qdb 发表于 2020-4-28 21:46
C语言的话用指针变量存储该字符串,然后用while语句对指针地址自加进行解引用,检测到当前地址内容为‘\0’ ...

大佬这是我后来改写的代码,可是运行结果报错了,你能帮忙看看错在哪儿了吗?
  1. #include <stdio.h>
  2. #include <string.h>
  3. void replace(char *str, char *s, char *t)
  4. {
  5.     char temp[256], *p;
  6.     int lens = strlen(s),
  7.         lent = strlen(t);
  8.         
  9.     p = strstr(str, s);     
  10.     strcpy(temp, p+lens);  
  11.     strcpy(p, t);           
  12.     strcpy(p+lent, temp);   
  13.         
  14. int main()
  15. {
  16.     char s[256] = "It is good to program in PASCAL language.";
  17.         
  18.     replace(s, "PASCAL", "C");
  19.     puts(s);
  20. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-4-28 22:03:49 | 显示全部楼层
sulley 发表于 2020-4-28 21:58
大佬这是我后来改写的代码,可是运行结果报错了,你能帮忙看看错在哪儿了吗?

replace()函数没有结束的大括号    }
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-4-28 22:10:41 | 显示全部楼层
#include <stdio.h>
#include <string.h>

#pragma warning( disable : 4996)
void replace(char *str, char *s, char *t)
{
        char temp[256], *p;
        int lens = strlen(s),
                lent = strlen(t);

        p = strstr(str, s);
        strcpy(temp, p + lens);
        strcpy(p, t);
        strcpy(p + lent, temp);
        }

        int main()
        {
                char s[256] = "It is good to program in PASCAL language.";

                replace(s, "PASCAL", "C");
                puts(s);
                while (1);
        }

用你给的程序,稍微改动一下,完全是可以的
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-4-28 22:15:46 | 显示全部楼层

void replace(char*, char*, char*) ”:无法将参数 264Project 7源.cpp从”const char[7] ”转换为”char*”
请问这个该怎么解决
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-4-28 22:34:34 | 显示全部楼层
sulley 发表于 2020-4-28 22:15
void replace(char*, char*, char*) ”:无法将参数 264Project 7源.cpp从”const char[7] ”转换为”c ...

你是运行哪个代码出现的这个错误;
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-4-28 22:36:38 | 显示全部楼层
C:\Users\Richard\Desktop\Shell\捕获.PNG
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-4-28 22:43:03 | 显示全部楼层
Richard149 发表于 2020-4-28 22:40
可以这样修改,应该是你的机器对于类型的检查比较严格。

可是系统还是会这样报错我用的是VS2019
严重性        代码        说明        项目        文件        行        禁止显示状态
错误        C2440        “初始化”: 无法从“const char [7]”转换为“char *”        Project7        C:\Users\lenovo\source\repos\Project7\Project7\源.cpp        20       
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-4-28 23:57:12 | 显示全部楼层
Richard149 发表于 2020-4-28 22:40
可以这样修改,应该是你的机器对于类型的检查比较严格。

非常感谢你今天的热心帮助!感谢感谢感谢!今天遇到的问题大多和我的软件要求你较高。总之十分感谢你的帮助啊!
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-4-30 11:51:50 | 显示全部楼层
sulley 发表于 2020-4-28 21:58
大佬这是我后来改写的代码,可是运行结果报错了,你能帮忙看看错在哪儿了吗?

第十二行后少了个}
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-7-4 14:54

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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