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

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

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

问题

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

  1. #include <cs50.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. int main(void)
  5. {
  6. string word = get_string("给我一个单词:" );
  7. int j = strlen(word);
  8. int i;
  9. char new[j + 1];
  10. new[0] = '\0';
  11. for (i = 0; i < j; i++)
  12. {
  13. if (word[i] == 'e')
  14. {
  15. strcat(new, "3");
  16. }
  17. else if (word[i] == 'i')
  18. {
  19. strcat(new, "1");
  20. }
  21. else if (word[i] == 'a')
  22. {
  23. strcat(new, "6");
  24. }
  25. else if (word[i] == 'o')
  26. {
  27. strcat(new, "0");
  28. }
  29. else
  30. {
  31. char p = word[i];
  32. strcat(new, p);
  33. }
  34. }
  35. printf("%s\n", new);
  36. }

我得到了错误:

  1. 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]
  2. strcat(new, word[i]);
  3. ^~~~~~~
  4. &
  5. /usr/include/string.h:149:70: note: passing argument to parameter '__src' here
  6. extern char *strcat (char *__restrict __dest, const char *__restrict __src)

我的目标是使最后的 else 语句将数组中的当前字母 word[i] 追加到变量 new[] 中,以拼出一个新单词,在这个新单词中,每个元音字母都被数字替代,数字方面我没有问题。但是最后的 else 语句似乎有问题,我搞不清楚为什么。

英文:

When running my program:

  1. #include <cs50.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. int main(void)
  5. {
  6. string word = get_string("Give me a word: " );
  7. int j = strlen(word);
  8. int i;
  9. char new[j + 1];
  10. new[0] = '
    #include <cs50.h>
  11. #include <stdio.h>
  12. #include <string.h>
  13. int main(void)
  14. {
  15. string word = get_string("Give me a word: " );
  16. int j = strlen(word);
  17. int i;
  18. char new[j + 1];
  19. new[0] = '\0';
  20. for (i = 0; i < j; i++)
  21. {
  22. if (word[i] == 'e')
  23. {
  24. strcat(new, "3");
  25. }
  26. else if (word[i] == 'i')
  27. {
  28. strcat(new, "1");
  29. }
  30. else if (word[i] == 'a')
  31. {
  32. strcat(new, "6");
  33. }
  34. else if (word[i] == 'o')
  35. {
  36. strcat(new, "0");
  37. }
  38. else
  39. {
  40. char p = word[i];
  41. strcat(new, p);
  42. }
  43. }
  44. printf("%s\n", new);
  45. }
  46. ';
  47. for (i = 0; i < j; i++)
  48. {
  49. if (word[i] == 'e')
  50. {
  51. strcat(new, "3");
  52. }
  53. else if (word[i] == 'i')
  54. {
  55. strcat(new, "1");
  56. }
  57. else if (word[i] == 'a')
  58. {
  59. strcat(new, "6");
  60. }
  61. else if (word[i] == 'o')
  62. {
  63. strcat(new, "0");
  64. }
  65. else
  66. {
  67. char p = word[i];
  68. strcat(new, p);
  69. }
  70. }
  71. printf("%s\n", new);
  72. }

I get the error:

  1. 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]
  2. strcat(new, word[i]);
  3. ^~~~~~~
  4. &
  5. /usr/include/string.h:149:70: note: passing argument to parameter '__src' here
  6. 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结尾的字符串的指针。

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

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

英文:

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

  1. char s[ 2 ] = { word[ i ], 0 };
  2. 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); 和前一行替换为:

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

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

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

  1. #include <cs50.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. int main(void)
  5. {
  6. string word = get_string("给我一个单词:");
  7. int i;
  8. int j = strlen(word);
  9. char new[j + 1];
  10. for (i = 0; i < j; i++)
  11. {
  12. if (word[i] == 'e')
  13. {
  14. new[i] = '3';
  15. }
  16. else if (word[i] == 'i')
  17. {
  18. new[i] = '1';
  19. }
  20. else if (word[i] == 'a')
  21. {
  22. new[i] = '6';
  23. }
  24. else if (word[i] == 'o')
  25. {
  26. new[i] = '0';
  27. }
  28. else
  29. {
  30. new[i] = word[i];
  31. }
  32. }
  33. new[i] = '
    #include <cs50.h>
  34. #include <stdio.h>
  35. #include <string.h>
  36. int main(void)
  37. {
  38.     string word = get_string("给我一个单词:");
  39.     int i;
  40.     int j = strlen(word);
  41.     char new[j + 1];
  42.     for (i = 0; i < j; i++)
  43.     {
  44.         if (word[i] == 'e')
  45.         {
  46.             new[i] = '3';
  47.         }
  48.         else if (word[i] == 'i')
  49.         {
  50.             new[i] = '1';
  51.         }
  52.         else if (word[i] == 'a')
  53.         {
  54.             new[i] = '6';
  55.         }
  56.         else if (word[i] == 'o')
  57.         {
  58.             new[i] = '0';
  59.         }
  60.         else
  61.         {
  62.             new[i] = word[i];
  63.         }
  64.     }
  65.     new[i] = '\0';
  66.     printf("%s\n", new);
  67. }
  68. ';
  69. printf("%s\n", new);
  70. }
英文:

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:

  1. 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:

  1. #include &lt;cs50.h&gt;
  2. #include &lt;stdio.h&gt;
  3. #include &lt;string.h&gt;
  4. int main(void)
  5. {
  6. string word = get_string(&quot;Give me a word: &quot; );
  7. int i;
  8. int j = strlen(word);
  9. char new[j + 1];
  10. for (i = 0; i &lt; j; i++)
  11. {
  12. if (word[i] == &#39;e&#39;)
  13. {
  14. new[i] = &#39;3&#39;;
  15. }
  16. else if (word[i] == &#39;i&#39;)
  17. {
  18. new[i] = &#39;1&#39;;
  19. }
  20. else if (word[i] == &#39;a&#39;)
  21. {
  22. new[i] = &#39;6&#39;;
  23. }
  24. else if (word[i] == &#39;o&#39;)
  25. {
  26. new[i] = &#39;0&#39;;
  27. }
  28. else
  29. {
  30. new[i] = word[i];
  31. }
  32. }
  33. new[i] = &#39;
    #include &lt;cs50.h&gt;
  34. #include &lt;stdio.h&gt;
  35. #include &lt;string.h&gt;
  36. int main(void)
  37. {
  38. string word = get_string(&quot;Give me a word: &quot; );
  39. int i;
  40. int j = strlen(word);
  41. char new[j + 1];
  42. for (i = 0; i &lt; j; i++)
  43. {
  44. if (word[i] == &#39;e&#39;)
  45. {
  46. new[i] = &#39;3&#39;;
  47. }
  48. else if (word[i] == &#39;i&#39;)
  49. {
  50. new[i] = &#39;1&#39;;
  51. }
  52. else if (word[i] == &#39;a&#39;)
  53. {
  54. new[i] = &#39;6&#39;;
  55. }
  56. else if (word[i] == &#39;o&#39;)
  57. {
  58. new[i] = &#39;0&#39;;
  59. }
  60. else
  61. {
  62. new[i] = word[i];
  63. }
  64. }
  65. new[i] = &#39;\0&#39;;
  66. printf(&quot;%s\n&quot;, new);
  67. }
  68. &#39;;
  69. printf(&quot;%s\n&quot;, new);
  70. }

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:

确定