如何使用string.h函数在C中用另一个单词替换单词 [英] How do I replace a word with another in C using string.h functions
本文介绍了如何使用string.h函数在C中用另一个单词替换单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
#include<stdio.h>
#include<string.h>
#include<ctype.h>
int main()
{
char text[100], cuv1[50], cuv2[50], text2[100];
char sep[] = " '\",.!?";
int i=0, j=0;
fgets(text, 100, stdin);
text[strlen(text) - 1] = '\0';
fgets(cuv1, 50, stdin);
cuv1[strlen(cuv1) - 1] = '\0';
fgets(cuv2, 50, stdin);
cuv2[strlen(cuv2) - 1] = '\0';
while(strstr( (text + i) , cuv1))
{
int a = strstr( (text + i) , cuv1) - (text + i);
int b = strstr( (text + i) , cuv1) - (text + i) + strlen(cuv1);
if( ( (a == 0) || strchr(sep, (text + i)[a - 1]) ) && ( ( (text + i)[b] == '\0') || strchr(sep, (text + i)[b]) ) )
{
int x;
for(x=0; x<a;>{
text2[j] = (text + i)[x];
j++;
}
i = i + b;
strcat(text2, cuv2);
printf("Textul 2 devine: %s\n", text2);
i = i + strlen(cuv1);
j = j + strlen(cuv2);
}
else
{
int x;
for(x=0; x<b;>{
text2[j] = (text + i)[x];
j++;
}
i = i + b;
}
}
printf("Textul final este: %s\n", text2);
return 0;
}
推荐答案
以旧方式提供一种可能的解决方案。
Here one possible solution in old fashion.
/* NOTE:
You must control de strOut length if not ...
or you are in trouble
the allocate space must be there
*/
int
replace( char *strIn, char *sStr, char *rStr, char *strOut)
{
/*
Validate input fields
PRECONDITION( strIn !=NULL );
PRECONDITION( strOut!=NULL );
PRECONDITION( sStr !=NULL );
PRECONDITION( rStr !=NULL );*/
int i=0,j=0;
int iLen = strlen( strIn);
int sLen = strlen( sStr );
int rLen = strlen( rStr );
//Trim trailing and heading spaces (optional)
//RTrim(sStr,sLen);
//LTrim(sStr,sLen);
for( i=0; i<iLen; i++) {
if( strncmp(&strIn[i], sStr, sLen)) strOut[j++]=strIn[i];
else {
memcpy( &strOut[j], rStr, rLen);
j+=rLen;
i+=(sLen-1);
}
}
strOut[j]=0;
return EXIT_SUCCESS;
}
int
main(int argc, char *argv[])
{
char in[]="jjjadhjhas jhasdjhdakdjah jhasdjhas";
char sStr[]="as";
char rStr[]="xxxx";
char out[200];
int retCode = replace( in, sStr, rStr, out);
fprintf(stderr, "Input String : %s\n", in);
fprintf(stderr, "Search String : %s\n", sStr);
fprintf(stderr, "Replace String: %s\n", rStr);
fprintf(stderr, "Result String : %s\n", out);
fprintf(stderr, "Return Code: %d\n", retCode);
exit(EXIT_SUCCESS);
}
就像那样:
Like that:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
void word_replace(char* str, char* word_old, char* word_new)
{
char* wd_buf = NULL;
if ((wd_buf = strstr(str, word_old)) != NULL)
{
int old_len = strlen(word_old);
int new_len = strlen(word_new);
int len = abs(old_len - new_len);
while (--len >= 0 && old_len < new_len)
{
int pos = strlen(str);
while (--pos >= wd_buf - str)
str[pos + 1] = str[pos];
}
memcpy((void*)&str[wd_buf - str], (const void*)word_new, new_len);
if (new_len < old_len)
{
memcpy((void*)&str[wd_buf - str + new_len],
(const void*)&str[wd_buf - str + old_len],
strlen(&str[wd_buf - str + old_len]));
}
}
}
int main(int argc, char* argv[])
{
char str[256] = "lazy dog jumps over a brown fox\0";
char word_old[256] = "dog";
char word_new[256] = "straus";
word_replace(str, word_old, word_new);
printf("%s\n", str);
_getch();
return 0;
}
这篇关于如何使用string.h函数在C中用另一个单词替换单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文