c - 如何将字符串与子字符串进行比较以将第一个和第二个之间的相等部分转换为'*'而不使用
问题描述
我必须请求第一个单词将其与第二个单词进行比较,并在不使用 <string.h> 库的情况下将所有出现的字符逐个替换为 '*' 工作字符。
练习:编写一个 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*
推荐阅读
- javascript - 联系表格(提交成功并重置)
- django - 如果只有另一个模型的字段包含某个值并且这两个模型具有外键,我如何过滤模型的对象
- php - Laravel 碳解析日期
- mariadb - ProxySQL 给我错误 Access denied for user
- android - Android Studio 在名称上给我一个错误,说它必须是 log.v 中的“throwable tr”。有人可以解释一下为什么
- arrays - 如何在arduino的char数组中放置变量?
- visual-studio - 用于编辑和继续的 Visual Studio 快捷键:暂停、(编译)然后再次运行
- vue.js - 样式化 vuetify 组件 v-expansion-panel__header
- bash - 如何仅在文件中保存 15 秒后的命令输出?
- android - 使用 Unity 在 ARCore 中移动游戏对象的锚点