【发布时间】:2016-03-08 16:20:07
【问题描述】:
我正在上编程课程,但在我的一小部分任务中有些东西我无法弄清楚。到目前为止,我已经编写了我需要的所有东西,我的代码是:
#include <stdio.h>
#include <string.h>
int main (void)
{
unsigned int length;
unsigned int length2;
char s1[15], s2[15];
puts("Hi! Enter a word to calculate its length!: ");
gets(s1);
length = strlen(s1);
printf("\n-> %s has %d characters!", s1, length);
puts("\n\nEnter another word to calculate its length!: ");
gets(s2);
length2 = strlen(s2);
printf("\n-> %s has %d characters!", s2, length2);
if(strcmp(s1,s2)==0)
{
puts("\n\n-> Words are equal!");
}
if(strcmp(s1,s2)<0)
{
puts("\n\n-> The second word is greater than the first one!");
}
if(strcmp(s1,s2)>0)
{
puts("\n\n-> The first word is greater than the second one!");
}
puts("\n\n...\n\n");
char _add[15];
puts("Type a word to add the first word!");
gets(_add);
strcat(s1, _add);
printf("\n%s added to the first word. Now the first word is:\n",_add);
puts(s1);
length = strlen(s1);
printf("\nNow the first word(%s) has %d characters!", s1, length);
char repl = '!';
//chrstr????
return 0;
}
我应该用“!”替换第三个连续的符号符号并显示新的 s2。但我坚持更换零件(在 chrsts 中???),我怎样才能用“第 3 个连续符号”替换它
edit// 另一个问题是我应该确认 s2 有 6 个或更多字符。我该怎么做?
【问题讨论】:
-
警告:不要使用
gets()。请改用fgets()。gets()容易受到缓冲区溢出的影响,不再是 C 的一部分。 -
如果
length>=6你的while循环永远不会终止。 -
你能举个例子吗?对于“aaaaabbccc”应该是“aa!!!bbcc!”还是“aa!aabbcc!”?
-
@Ashiquzzaman 我更改了描述。
-
@AlexLop。我不知道。任务中没有给出任何东西。它只是说“在 s2 字符串中,如果它的长度大于 6 个字符,请将第 3 个连续符号替换为 '!'符号;显示新字符串 s2"