做无元音cs50问题并遇到strcat错误

问题描述 投票:0回答:3

运行我的程序时:

#include <cs50.h>
#include <stdio.h>
#include <string.h>

int main(void)
{
    string word = get_string("Give me a word: " );
    int j = strlen(word);
    int i;
    char new[j + 1];

    new[0] = '\0';
    
    for (i = 0; i < j; i++)
    {
        if (word[i] == 'e')
        {
            strcat(new, "3");
        }
        else if (word[i] == 'i')
        {
            strcat(new, "1");
        }
        else if (word[i] == 'a')
        {
            strcat(new, "6");
        }
        else if (word[i] == 'o')
        {
            strcat(new, "0");
        }
        else
        {
            char p = word[i];
            strcat(new, p);
        }
    }
    printf("%s\n", new);
}

我收到错误:

no-vowels-test.c:39:25: error: incompatible integer to pointer conversion passing 'char' to parameter of type 'const char *'; take the address with & [-Werror,-Wint-conversion]
            strcat(new, word[i]);
                        ^~~~~~~
                        &
/usr/include/string.h:149:70: note: passing argument to parameter '__src' here
extern char *strcat (char *__restrict __dest, const char *__restrict __src)

我的目标是使最后一个

else
语句将
word[i]
数组中的当前字母附加到变量
new[]
中,以拼出一个新单词,其中每个元音都被数字替换,这些数字我没有问题与.但最后一个
else
的说法似乎有问题,我不明白为什么。

arrays c cs50 strcat
3个回答
1
投票

要使用

strcat
,您需要提供一个指向以 NUL 结尾的字符串的指针。

char s[ 2 ] = { word[ i ], 0 };
strcat( new, s );

但是您不需要

strcat
来添加单个字符。特别是因为您已经知道写字的位置。您所需要的只是
new[ i ] = c;
。完成后不要忘记用 NUL 终止字符串。


0
投票

strcat
的第二个参数必须是C字符串,即:以空字节结尾的
char
数组,而不是单个
char
,例如
p

您可以使用

strncat
解决此问题:将
strcat(new, p);
和上一行替换为:

    strncat(new, &word[i], 1);

这将从

char
开始的字符串中连接至多 1 个
word[i]

另一种方法是使用单独的变量作为

new
数组的索引:

#include <cs50.h>
#include <stdio.h>
#include <string.h>

int main(void)
{
    string word = get_string("Give me a word: " );
    int i, j;
    int len = strlen(word);
    char new[len + 1];

    for (i = j = 0; i < j; i++)
    {
        if (word[i] == 'e')
        {
            new[j++] = '3';
        }
        else if (word[i] == 'i')
        {
            new[j++] = '1';
        }
        else if (word[i] == 'a')
        {
            new[j++] = '6';
        }
        else if (word[i] == 'o')
        {
            new[j++] = '0';
        }
        else
        {
            new[j++] = word[i];
        }
    }
    new[j] = '\0';
    printf("%s\n", new);
}

-1
投票

一个简单的解决方案是逐个字符地修改输入字符串。以下示例使用 Ada 编程语言,该语言不属于 CS50 课程的一部分,但您应该可以理解。

with Ada.Text_IO; use Ada.Text_IO;

procedure Main is
begin
   Put ("Enter a word: ");
   declare
      word : String := Get_Line;
   begin
      for char of word loop
         case char is
            when 'A' | 'a' =>
               char := '6';
            when 'E' | 'e' =>
               char := '3';
            when 'I' | 'i' =>
               char := '1';
            when 'O' | 'o' =>
               char := '0';
            when 'U' | 'u' =>
               char := '9';
            when others =>
               null;
         end case;
      end loop;
      Put_Line (word);
   end;
end Main;

我没有看到可以替代“u”的值,所以我使用了“9”。以下是程序的执行示例:

Enter a word: The Quick Brown Fox Jumped Over The Lazy Dogs Back.
Th3 Q91ck Br0wn F0x J9mp3d 0v3r Th3 L6zy D0gs B6ck.
© www.soinside.com 2019 - 2024. All rights reserved.