【问题标题】:Character Handling and String Manipulation字符处理和字符串操作
【发布时间】:2014-11-14 18:18:26
【问题描述】:

我刚开始学习 C。 我正在制作以下程序:

#include<stdio.h>
#include<ctype.h>

int main()
{
    char s1[1000];
    char s2 [1000];
 /*    void countchar (const char * const);  */
    void reverse(const char * const);
    char manipulate (char , const char);

    printf ("Enter sentence number 1:\n");
gets (s1);
printf ("\nEnter sentence number 2:\n");
gets (s2);

printf ("\nThe two sentences entered are:\n1. %s\n2. %s\n\n", s1, s2);

/*    printf ("_______________________________________\n\n");
countchar (s1, s2);     */

printf ("_______________________________________\n\n"
        "The first sentence reversed is:\n");
reverse (s1);
printf ("\n________________________________________\n");

manipulate (s1, s2);

return 0;
}

void reverse (const char * const s1Ptr)
{
    if (s1Ptr [0] == '\0')
        return;
    else /*{
           if (isupper (s1Ptr [0]))
                tolower (s1Ptr [0]);
                    else toupper (s1Ptr [0]);
*/
    reverse (&s1Ptr[1]);
    putchar (toupper (s1Ptr [0]));
}


char manipulate(char s1, const char s2)
{
printf ("10 characters of first sentence + 10 characters of second sentence:\n%s", strncat (s1, s2, 10));
}

程序会读两句,然后 计算字符数, 反转第一句并将所有小写转换为大写,反之亦然, 并将第二段的 10 个字符附加到第一段。

附加字符的操作函数不起作用。你能帮我修一下吗? 在函数 reverse 中,如何将大小写反之亦然?因为我只能从小写到大写

带有/*的部分是因为我不知道如何制作函数。我真的需要帮助,请指出我的错误并帮助我解决它们。 谢谢。

【问题讨论】:

  • 它的签名是错误的。应该是char*s,而不是单个chars。
  • void reverse (const char * const s1Ptr) 不应是 const 参数,因为该函数会更改 *s1Ptr(尽管已注释掉)。这个函数也是递归的,这是你想要的吗?注意 - 有库函数 strupr()strlwr() 对 char 数组(字符串)进行操作。为什么要重新发明轮子?
  • char manipulate (char , const char); --> char 操作 (char *, const char *);和`#include "
  • @RedAlert 在哪一行?
  • @WeatherVane 那么你推荐的论点是什么?是的,我打算它是递归的。 strupr() 和 strlwr() 没学过,想学习实现 toupper 和 tolower。你能帮我解决这个问题吗?

标签: c string character


【解决方案1】:

我已将 cmets 添加到您发布的代码中:

void reverse (const char * const s1Ptr)  // don't define as const
{
    if (s1Ptr [0] == '\0')               // testing agaist 0 will do
        return;
    else /*{
        if (isupper (s1Ptr [0]))
            tolower (s1Ptr [0]);         // nothing done with the result
        else toupper (s1Ptr [0]);        // nothing done with the result
*/
    reverse (&s1Ptr[1]);                 // recursion? A simple loop would do
    putchar (toupper (s1Ptr [0]));       // OP: "Function will only uppercase it"
}

使用更简单的指向字符的方式重写了这段代码。您仍然需要反转两个字符串中的一个,但我会让您自己弄清楚。

void reverse (char * s1Ptr)              // don't define as const
{
    if (*s1Ptr == 0)                     // terminate recursion
        return;
    else {
        if (isupper (*s1Ptr))            // simpler way of writing s1Ptr[0]
             *s1Ptr = tolower (*s1Ptr);  // put result back in the string
        else *s1Ptr = toupper (*s1Ptr);  // because you manipulate it later
    }
    putchar (*s1Ptr);                    // swapped last two lines round
    reverse (s1Ptr+1);                   // simpler way of pointing to next character
}

“用于附加字符的操作功能不起作用。”是的!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-25
    • 1970-01-01
    • 1970-01-01
    • 2013-06-17
    • 1970-01-01
    • 2010-10-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多