在处理不包含元音字母的 CS50 问题时,遇到了关于 strcat 的错误。

huangapple go评论80阅读模式
英文:

doing the no-vowels cs50 problem and ran into an error with strcat

问题

在运行我的程序时,我遇到了错误:

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

int main(void)
{
    string word = get_string("给我一个单词:" );
    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 语句似乎有问题,我搞不清楚为什么。

英文:

When running my program:

#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] = '
#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);
}
'; 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); }

I get the error:

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)

My goal here is to make the last else statement append the current letter in the array of word[i] into the variable new[] to spell out a new word where every vowel is replaced by a number, the numbers I have no problem with. But the last else statement seems to have a problem and I cant figure out why.

答案1

得分: 2

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

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

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

英文:

To use strcat, you would need to provide a pointer to a NUL-terminated string.

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

But you don't need strcat to add a single character. Especially since you already know the position at which to write the character. All you need is new[ i ] = c;. Just don't forget to terminate your string with a NUL once you're done.

答案2

得分: 1

第二个参数传递给 strcat 必须是一个C字符串,即以空字符结尾的 char 数组,而不是一个单独的 char,比如 p

你可以使用 strncat 来解决这个问题:将 strcat(new, p); 和前一行替换为:

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

这会将起始于 word[i] 的字符串的最多1个 char 连接起来。

另一种方法是直接设置 new 中的字节:

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

int main(void)
{
    string word = get_string("给我一个单词:");
    int i;
    int j = strlen(word);
    char new[j + 1];

    for (i = 0; i < j; i++)
    {
        if (word[i] == 'e')
        {
            new[i] = '3';
        }
        else if (word[i] == 'i')
        {
            new[i] = '1';
        }
        else if (word[i] == 'a')
        {
            new[i] = '6';
        }
        else if (word[i] == 'o')
        {
            new[i] = '0';
        }
        else
        {
            new[i] = word[i];
        }
    }
    new[i] = '
#include <cs50.h>
#include <stdio.h>
#include <string.h>

int main(void)
{
    string word = get_string("给我一个单词:");
    int i;
    int j = strlen(word);
    char new[j + 1];

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

The second argument to strcat must be a C string, ie: an array of char terminated with a null byte, not a single char such as p.

You can fix this problem with strncat: replace strcat(new, p); and the previous line with:

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

This concatenates at most 1 char from the string starting at word[i].

Another approach would set the bytes in new directly:

#include &lt;cs50.h&gt;
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;

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

    for (i = 0; i &lt; j; i++)
    {
        if (word[i] == &#39;e&#39;)
        {
            new[i] = &#39;3&#39;;
        }
        else if (word[i] == &#39;i&#39;)
        {
            new[i] = &#39;1&#39;;
        }
        else if (word[i] == &#39;a&#39;)
        {
            new[i] = &#39;6&#39;;
        }
        else if (word[i] == &#39;o&#39;)
        {
            new[i] = &#39;0&#39;;
        }
        else
        {
            new[i] = word[i];
        }
    }
    new[i] = &#39;
#include &lt;cs50.h&gt;
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
int main(void)
{
string word = get_string(&quot;Give me a word: &quot; );
int i;
int j = strlen(word);
char new[j + 1];
for (i = 0; i &lt; j; i++)
{
if (word[i] == &#39;e&#39;)
{
new[i] = &#39;3&#39;;
}
else if (word[i] == &#39;i&#39;)
{
new[i] = &#39;1&#39;;
}
else if (word[i] == &#39;a&#39;)
{
new[i] = &#39;6&#39;;
}
else if (word[i] == &#39;o&#39;)
{
new[i] = &#39;0&#39;;
}
else
{
new[i] = word[i];
}
}
new[i] = &#39;\0&#39;;
printf(&quot;%s\n&quot;, new);
}
&#39;; printf(&quot;%s\n&quot;, new); }

huangapple
  • 本文由 发表于 2023年7月31日 22:17:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76804495.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定