【问题标题】:Replacing consecutive symbol with another用另一个替换连续符号
【发布时间】: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&gt;=6 你的while 循环永远不会终止。
  • 你能举个例子吗?对于“aaaaabbccc”应该是“aa!!!bbcc!”还是“aa!aabbcc!”?
  • @Ashiquzzaman 我更改了描述。
  • @AlexLop。我不知道。任务中没有给出任何东西。它只是说“在 s2 字符串中,如果它的长度大于 6 个字符,请将第 3 个连续符号替换为 '!'符号;显示新字符串 s2"

标签: c char symbols


【解决方案1】:

“如何用第三个连续符号替换它”来替换s2中的第三个字符,你可以使用array indexing,即用这个替换\\chrstr????这一行:

s2[2] = '!';

(当然也可以打印出来,如果你想要的话)

关于如何确认 s2 有 6 个或更多字符,只需使用 if 语句:

if (length2 < 6) {
    /* do something to indicate string is too small */
}

【讨论】:

    【解决方案2】:

    关于如何确认 S2 有 6 个或更多字符的问题,应该是:

       if(strcmp(s2)>=6)
        {
            puts("\n\n-> S2 has 6 or more characters!!!");
        }
    

    现在,如果你想识别你的第三个字符,它会是这样的:

    #include<stdio.h>
    #include<string.h>
    int main(){
        char s2[20];
        char third;
    
        printf("Hi! Enter a word so i can tell u its third character:  ");
        gets(s2);
        third  = s2[2];//it starts in 0, so your 3rd char is number "2"
        printf("\n-> The 3rd character is: %c", third);
    }
    

    【讨论】:

    • 这一行:if(strcmp(s2)&gt;=6) 是错误的。 strcmp() 函数比较两个字符串。对于这个电话,应该使用strlen()
    【解决方案3】:

    将第三个连续字符更改为“!”的可能算法(如果我理解正确的话):

    1.   Set S2 iteration index (IDX) to 0
    2.   While length of S2 > IDX + 2 do
    2.1     If S2[IDX] == S2[IDX+1] == S2[IDX+2] then
    2.1.1       S2[IDX+2] = '!'
    2.1.2       IDX = IDX + 3 
    2.2     Else
    2.2.1       IDX = IDX + 1
    

    关于验证s2有6个或更多字符:

    if (strlen(s2) < 6)
    {
        // error message
    }
    

    【讨论】:

      猜你喜欢
      • 2017-01-19
      • 2023-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-01
      • 1970-01-01
      • 2014-05-31
      • 2015-11-15
      相关资源
      最近更新 更多