gpt4 book ai didi

c - 如何在不使用 的情况下将字符串与子字符串进行比较以将第一个和第二个之间的相等部分转换为 '*'

转载 作者:行者123 更新时间:2023-12-04 03:48:28 25 4
gpt4 key购买 nike

我必须请求第一个词以将其与第二个词进行比较,并在不使用 库的情况下逐个字符地替换所有出现的“*”。

练习:编写一个 C 程序,接收从键盘输入的两个单词作为输入。考虑到每个单词最多可以包含 30 个字符。该程序必须区分大小写,即它必须区分小写字母和大写字母,并且还必须能够分析数字、符号和标点符号。程序必须用“*”字符替换第一个单词中出现的第二个单词。比如输入单词

abchdfffchdchdtlchd

and

chd

程序应该显示单词

ab*fff**tl*
#include <stdio.h>
#include <stdlib.h>

#define MAX 30

int main()
{
char string1 [MAX+1], string2 [MAX+1],replace = '*';
int nChar1 = 0, nChar2 = 0, flag = 0, h=0;

printf ("Enter a word (max 30 characters): ");

scanf ("%[^\n ]", &string1);
fflush (stdin);

printf ("\nYou wrote this word: %s\n", string1);

for (int i=0; i<(MAX+1); i++)
{
if (string1[i] == '\0')
break;
else
nChar1++;
}

printf ("The characters are: %d\n", nChar1);

printf ("\nEnter a word you want to change with '*' in the first string: ");

scanf ("%[^\n ]", &string2);
fflush (stdin);

printf ("\nYou wrote this word: %s\n", string2);

for (int j=0; j<(MAX+1); j++)
{
if (string2[j] == '\0')
break;
else
nChar2++;
}

printf ("The characters are: %d\n", nChar2);

for (int i=0, j=0, z=0; i<nChar1, j<nChar2; i++, j++)
{
if (string1[i] == string2[j])
{
for (int k=0; k<nChar2; k++)
{
if (string1[i+k] == string2[j+k])
flag++;
else
flag=0;
}
}

j=0;

if (flag == nChar2)
{
string1[h] = replace;
h++;
}
else
{
h++;
}
string1[z+1] = string1[h];
}

printf("\n%s", string1);

return 0;
}

最佳答案

将任务分解为几个独立的函数。

一个函数将计算传递的字符串的长度。另一个函数将在字符串中找到一个子字符串。第三个函数将用一个字符替换目标子字符串。

这是一个演示程序。

#include <stdio.h>

size_t length( const char *s )
{
size_t n = 0;

while ( *s++ ) ++n;

return n;
}

char * find_substring( const char *s1, const char *s2 )
{
size_t n1 = length( s1 );
size_t n2 = length( s2 );

const char *target = NULL;

if ( ( *s2 != '\0' ) && !( n1 < n2 ) )
{
for ( size_t i = 0, n = n1 - n2 + 1; !target && i < n; i++ )
{
if ( s1[i] == s2[0] )
{
size_t j = 1;
while ( j != n2 && s1[i+j] == s2[j] ) ++j;
if ( j == n2 ) target = s1 + i;
}
}
}

return ( char * )target;
}

char * replace( char *s1, const char *s2, char c )
{
int done = 0;
size_t n2 = length( s2 );

for ( char *p = s1, *q = s1; !done; )
{
char *tmp = find_substring( q, s2 );

if ( tmp == NULL )
{
if ( p != q )
{
while ( ( *p++ = *q++ ) );
}
done = 1;
}
else
{
if ( p == q )
{
p = tmp;
}
else
{
while ( q != tmp ) *p++ = *q++;
}
*p++ = c;
q = tmp + n2;
}
}

return s1;
}

int main(void)
{
{
char s1[] = "abc";
const char *s2 = "chd";

puts( replace( s1, s2, '*' ) );
}

{
char s1[] = "achd";
const char *s2 = "chd";

puts( replace( s1, s2, '*' ) );
}

{
char s1[] = "chda";
const char *s2 = "chd";

puts( replace( s1, s2, '*' ) );
}

{
char s1[] = "chd";
const char *s2 = "chd";

puts( replace( s1, s2, '*' ) );
}

{
char s1[] = "abchdfffchdchdtlchd";
const char *s2 = "chd";

puts( replace( s1, s2, '*' ) );
}

return 0;
}

程序输出为

abc
a*
*a
*
ab*fff**tl*

关于c - 如何在不使用 <string.h> 的情况下将字符串与子字符串进行比较以将第一个和第二个之间的相等部分转换为 '*',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64752712/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com