【发布时间】: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。你能帮我解决这个问题吗?