我有一个名为“line”的字符串,其中包含一个单词。这个词每次都是随机的,我想要一个循环,可以用星号替换随机词中的每个字母。到目前为止,我有一个可以用星号替换字母 e 的循环。有没有办法修改这个循环,以便替换所有字母,而不是仅仅用剩下的 25 个字母复制和粘贴这个循环 25 次?还有大写字母?
非常感谢。
for (w = 0; w <= strlen(line); w++)
{
if (line[w] == 'e')
{
line[w] = '*';
}
}
该程序将替换"word"
为"****"
#include <stdio.h>
#include <string.h>
int main(void)
{
char text[] = "This a paragraph of words where a random word should be replaced with * instead of word when word is found multiple times.";
char find[] = "word ";
printf("Start: %s\n", text);
while(strstr(text, find))
{
memset(strstr(text,find), '*', strlen(find)-1);
}
printf("Result: %s\n", text);
return 0;
}
Success #stdin #stdout 0s 9424KB
Start: This a paragraph of words where a random word should be replaced with * instead of word when word is found multiple times.
Result: This a paragraph of words where a random **** should be replaced with * instead of **** when **** is found multiple times.
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句