c语言程序设计 单词替换

作者&投稿:枞旭 (若有异议请与网页底部的电邮联系)
c语言 实现对句子中的指定单词进行指定替换~

以前回答过
程序中各字符串数组的大小可根据实际需要更改,不影响运行。
#include
#include
main()
{ char string[30],change[5],replace[5];
char s[30];
char *p,*q;
int i,lengthch,lengthre,flag,j=0;
printf("Input a string:");
gets(string);
printf("Input the string you want to be changed:");
scanf("%s",change);
printf("Input the string you want to replace:");
scanf("%s",replace);
lengthch=strlen(change);
lengthre=strlen(replace);
puts(string);
p=q=string;
for(;*p;p++)
{if(*p==change[0])
{
flag=1;
q=p;
for(i=0;i<lengthch;i++)
{ if(*q!=change[i])
{ flag=0;
break;
}
else q++;
}
if(flag==1)
{ for(i=0;i<lengthre;i++)
s[j++]=replace[i];
p=q-1;
}
}
else s[j++]=*p;
}
s[j]='\0';
puts(s);
}
http://zhidao.baidu.com/question/406414478.html?oldq=1

#include
#include

int main()
{
int i,k,n=0,j,m=0,lena,lenb;
char a[266];
char b[100];


scanf("%s",a);
scanf("%s",b);//b的格式控制符有问题

lena=strlen(a);
lenb=strlen(b);


for(i=0;i<lena;i++)
{
if(b[0]==a[i])
{
j=i;
break;
}
}
printf("替换之前的a:");
printf("%s
",a);

/* for(j=i;j<i+lenb;j++)
{ //你这里这样写是不科学的..如果i+lenb>lena怎么办??
if(a[j]==b[m])
m++;
}*/

for (j=i;j<lena;j++)
{
a[j]=b[m];
m++;
}

if(m==lenb-1)
{
for(k=0;k<i;k++)
{
printf("%c",a[k]);
}

}
printf("替换之后的a:");
printf("%s
",a);
printf("替换之前的b:");
printf("%s
",b);

return 0;
}
要注意啊..程序风格问题...多写一点注释...

//查找并替换指定文件所包含的字符串,保存于另一个文件中

#include "stdio.h"
#include "iostream.h"
#include "string.h"

void main()
{
FILE *fp1,*fp2;
char path[128],path_new[128],search[128],replace[128];

cout<<"input the file path and name."<<endl;//输入原文件的路径和文件名
gets(path);
//puts(path);
cout<<"input the new file path and name."<<endl;//输入新文件的路径和文件名
gets(path_new);
cout<<"input the words you want to search."<<endl;//输入所查找的字符串
gets(search);
cout<<"input the words you want to replace."<<endl;//输入要替换的字符串
gets(replace);

int len;//所查找的字符串的长度
len=strlen(search);
//cout<<len<<endl;
int len_replace;//要替换的字符串的长度
len_replace=strlen(replace);

if((fp1=fopen(path,"r+"))==NULL)//打开原文件
{
cout<<"Can not open this file."<<endl;
//exit();
}

if((fp2=fopen(path_new,"w+"))==NULL)//创建新文件
{
cout<<"Can not open this file."<<endl;
//exit();
}

while(feof(fp1)==0)
{
char ch=fgetc(fp1);
long num=1;//匹配字符计数器

if(ch==search[0])//发现与查找的字符串匹配的第一个字符
{
int err=0;
for(int i=1;i<len;i++)//确定字符串是否完全匹配
{
ch=fgetc(fp1);
num++;
if(ch!=search[i])
{
err=1;
break;
}
}

if(err==0) //完全匹配
{
for(i=0;i<len_replace;i++)//插入要替换的字符串
fputc(replace[i],fp2);
}

else//未完全匹配,返回指针至与查找的字符串匹配的第一个字符
{
fseek(fp1,-num,1);
ch=fgetc(fp1);
fputc(ch,fp2);
}

}

else fputc(ch,fp2);//复制其他字符串

}

fclose(fp1);//关闭原文件
fclose(fp2);//关闭新文件

}

-------------------
C++的程序可以吗?已经通过调试,希望对你有帮助。

 
 
 
以下是我写的代码,你可以参考。
代码里只有简单的错误处理(好让程序的简单运作显见)。
要合理定义函数的话,可以把
(1)计算文件字符个数和
(2)读取文件、替换单词然后保存内容
的代码写成个别的函数。

有问题尽管问。

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

#define SSIZE 99

void main() {
    char ifname[SSIZE], ofname[SSIZE] = "modified_", old[SSIZE], new[SSIZE];
    FILE *ifp, *ofp;

    puts("请输入文本文件名、替换前的单词和替换后的单词(用回车把每一项输入隔开):");
    strcat(ofname, gets(ifname));    gets(old);    gets(new);

    if ((ifp = fopen(ifname, "r")) && (ofp = fopen(ofname, "w"))) {
        size_t ifsize = 0, count = 0;
        char *text, *found;

        /* 计算输入文件的大小,然后把全部内容读入动态内存,即得一个字符串 */
        for ( ; getc(ifp) && ! feof(ifp); ++ifsize);
        rewind(ifp);
        fscanf(ifp, "%[^\0]", text = malloc(ifsize * sizeof(char) + 1));

        if (found = strlen(old) ? strstr(text, old) : NULL) {
            for ( ; found = strstr(text, old); text = found + strlen(old), ++count) {
                fprintf(ofp, "%.*s", found - text, text);
                fputs(new, ofp);
            }
            fputs(text, ofp);
        }
        else
            fputs(text, ofp);

        printf("替换的个数是 %d。\n替换后的内容保存在 %s 里。\n", count, ofname);
    }
    else
        printf("什么也没做成,因为打不开这个文件:%s\n", ifp ? ofname : ifname);
}
 
 
 


高陵县13564126361: 单词替换用C语言写的! -
栾食安理: #include<stdio.h>#include<windows.h> int main(){ printf("hi,you,are"); sleep(1000);//只有延时才能看到由hi,you,are变为hello,your,is?,否则会变为hello,your,is?如果用VS2013 IDE的话就把sleep改成sleep_s system("CLS");//调用windows清屏命令 printf("hello,your,is?"); return 0;

高陵县13564126361: 题目 - 例题(12.8) 单词替换 C语言描述输入一个字符串,以回车结束(字符串长度关于输入输入包括3行: 第1行是包含多个单词的字符串 s 第2行是待替换... -
栾食安理:[答案] #include #define MAX 1000 #define LEN 20 void Input (char *a, char *b, char *c){ gets(a); gets(b); gets(c); } void Swap (char *... //length of the old word SwapWordLength=strlen(SwapWord); //length of the new word n=SwapWordLength-FindWordLength;...

高陵县13564126361: c语言替换字符串中某个单词,请给出代码,谢谢. -
栾食安理: #include#include#include int main() { char str[1000]; char *str1=NULL; char *str2=NULL; char strtemp[100]; char str3[100]; puts("请输入初始字符串:"); gets(str); puts("请输入要被替换掉的字符串:"); gets(strtemp); puts("请输入要...

高陵县13564126361: 如何编辑c语言程序把一个单词译成另一个单词 -
栾食安理: #include<stdio.h> main() { char c1,c2,c3,c4,c5,d1,d2,d3,d4,d5; c1=C; c2=h; c3=i; c4=n; c5=a; d1=c1+4; d2=c2+4; d3=c3+4; d4=c4+4; d5=c5+4; printf("%d%d%d%d%d",d1,d2,d3,d4,d5); } 只要ASCII码加4就可以了,哪里不懂再问我,QQ:527709313

高陵县13564126361: 请用C语言编写的程序: 将一段英文中的要查找的单词换成要代替的单词(要支持大小写英文字母)! 如: -
栾食安理: #include<stdio.h>#include<string.h> void change(char c[]) { c[strlen(c)]=' '; char s[20]={0}; int j=0; for(int i=0;i<strlen(c);i++) { if(c[i]==' '&&strcmp(s,"you")==0) {printf("ni ");j=0;memset(s,'\0',sizeof(s));continue;} if(c[i]==' '&&strcmp(s,"I")==0) {...

高陵县13564126361: 用c语言完成单词替换 -
栾食安理: 按照自己的理解写了一段代码,请根据自己需要进行修改.*查找单词有空格或非字母时给出警告*替换单词有空格或非字母时给出警告*语句中标点可以同单词没有空格#include <stdio.h>#include <string.h>#define STRLEN 100 int main() { char ...

高陵县13564126361: C语言文件中单词的查找与替换
栾食安理: /* 功能:查找替换函数 返回值:替换后的字符串 strretu */ char *Replace(char *str, char *str1, char *str2, char *strretu) { char *str_temp = str; //源字符串 char *str1_temp = str1; //要求替换的字符串 char *str2_temp = str2; //替换成字符串 char *...

高陵县13564126361: 用c语言 查找英文句子中并替换某个单词 -
栾食安理: #include #include char str[10] = "111222333";// 字符串替换(源字符串, 旧字符串, 新字符串)cha...

高陵县13564126361: 如何编辑c语言程序把一个单词译成另一个单词 -
栾食安理: main() { char c1,c2,c3,c4,c5,d1,d2,d3,d4,d5; c1=C; c2=h; c3=i; c4=n; c5=a; d1=c1+4; d2=c2+4; d3=c3+4; d4=c4+4; d5=c5+4; printf("%d%d%d%d%d",d1,d2,d3,d4,d5); } 只要ASCII码加4就可以了,哪里不懂再问我,QQ:527709313

高陵县13564126361: 题目描述 (请用C语言) 编写一个C程序实现将字符串中的所有单词"you"替换成"we" 输入 -
栾食安理: #include "stdio.h" #include "string.h" bool IsSeparator(char ch) {return (!((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))); } char* GetFirstWord(char *sentence) {char word[100];int i;for(i = 0; i < 100; i++)word[i] = '\0';for(i = 0; ...

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网