在C语言中将字符串转换为小写/计算单词时出现问题

在C语言中将字符串转换为小写/计算单词时出现问题,c,char,constants,tolower,C,Char,Constants,Tolower,在我的第三个函数中,我在const char*it=s处得到一个语法错误;当我试图编译时。我的朋友让这个程序在他的编译器上运行,但是我使用的是Visual Studio,它不会编译。任何和所有的帮助将不胜感激 #include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <string.h> //Declaring program functions char concSt

在我的第三个函数中,我在const char*it=s处得到一个语法错误;当我试图编译时。我的朋友让这个程序在他的编译器上运行,但是我使用的是Visual Studio,它不会编译。任何和所有的帮助将不胜感激

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


//Declaring program functions
char concStrings(); 
int compStrings();
int lowerCase();
int birthday();

//Main function begins the program execution
int main (void)
{
concStrings( );
compStrings();
lowerCase();
birthday();

}
//end main function

//concatenate function
char concStrings ( )
{
char string1[20];
char string2[20];
char string3[50] = "";;

printf("Enter in two strings:");
gets(string1);
gets(string2);

strcat(string3, string1);
strcat(string3, "-");
strcat(string3, string2);

printf("The new string is: %s\n", string3);

}

// compare function
int compStrings() 
{
char string1[20];
char string2[20];
int result;

printf ( "Enter two strings: ");
//  scanf( "%s%s", string1, string2 );
gets(string1);
gets(string2);

result = strcmp ( string1, string2 );

if (result>0)
printf ("\"%s\"is greater than \"%s\"\n", string1, string2 );
else if (result == 0 )
printf ( "\"%s\" is equal to \"%s\"\n", string1, string2 );
else
printf ( "\"%s\" is less than \"%s\"\n", string1, string2);
return 0;
}

//lowercase function
int lowerCase()
{
char s[300];
int x;
int counted = 0;
int inword = 0;

printf ("Enter a sentence:");
//  scanf("%s", s);
gets(s);

const char* it = s;

printf ("\nThe line in lowercase is:\n");

for (x = 0; s[x] !='\0'; x++)
printf ("%c", tolower(s[x]));

do
{ 
switch(*it) 
{ 
  case '\0': case ' ': case '\t': case '\n': case '\r':
    if (inword)
   {
     inword = 0; counted++;
    }
    break; 
  default: 
    inword = 1;
 } 
 }
while(*it++);

printf("\nThere are %i words in that sentence.\n", counted);

return 0;
}

int birthday()
{
}
#包括
#包括
#包括
#包括
//声明程序函数
char concStrings();
int compStrings();
int小写();
int生日();
//主函数开始执行程序
内部主(空)
{
concStrings();
compStrings();
小写();
生日();
}
//终端主功能
//串联函数
字符字符串()
{
charstring1[20];
charstring2[20];
字符字符串3[50]=“”;;
printf(“输入两个字符串:”);
获取(string1);
获取(string2);
strcat(string3、string1);
strcat(string3,“-”);
strcat(第3列、第2列);
printf(“新字符串是:%s\n”,string3);
}
//比较函数
int compStrings()
{
charstring1[20];
charstring2[20];
int结果;
printf(“输入两个字符串:”);
//scanf(“%s%s”,string1,string2);
获取(string1);
获取(string2);
结果=strcmp(string1,string2);
如果(结果>0)
printf(“\%s\”大于\%s\”\n”,字符串1,字符串2);
否则如果(结果==0)
printf(“\%s\”等于\%s\“\n”,字符串1,字符串2);
其他的
printf(“\%s\”小于\%s\”\n”,字符串1,字符串2);
返回0;
}
//小写函数
int小写()
{
chars[300];
int x;
整数=0;
int-inword=0;
printf(“输入一个句子:”);
//scanf(“%s”,s);
获取(s);
常量字符*it=s;
printf(“\n小写的行是:\n”);
对于(x=0;s[x]!='\0';x++)
printf(“%c”,tolower(s[x]);
做
{ 
开关(*it)
{ 
案例“\0”:案例“”:案例“\t”:案例“\n”:案例“\r”:
如果(中文)
{
inword=0;计数++;
}
打破
违约:
inword=1;
} 
}
而(*it++);
printf(“\n该句子中有%i个单词。\n”,已计算);
返回0;
}
int生日()
{
}
C89(MSVC遵循C89标准)禁止在块中混合声明和语句:

gets(s);
const char* it = s;
您对
it
对象的声明是在
get
函数调用之后进行的,这是C89不允许的


此外,在最新的C标准中,
gets
函数已被删除,在所有C程序中应不惜一切代价避免使用。

在变量声明块的开头执行。对于旧C

改为

int lowerCase()
{
char s[300];
int x;
int counted = 0;
int inword = 0;
const char* it;

printf ("Enter a sentence:");
//  scanf("%s", s);
gets(s);

it = s;


我认为任何人都不应该使用
get
-(对不起,我只是按照我的教授给我的示例代码。所以最好使用这个?scanf(“%s”,s);我是否需要将它移到其他地方?对不起,我显然是新的。实际上,我在上一篇文章发表几分钟后收到了它,但非常感谢您的帮助。
rename progname.c progname.cpp