C 回文程序
我仍然是一个初学者,并且我在编写程序来检查字符串是否是回文时遇到了困难,因为我只使用C 回文程序,c,string,C,String,我仍然是一个初学者,并且我在编写程序来检查字符串是否是回文时遇到了困难,因为我只使用和库 字符串最多可以包含30个字符。我们必须假设字符串可以包含数字、标点符号和字母。另外,假设字符串中没有空格。必须区分大小写字符。所以“夫人”是回文,但“夫人”不是回文。字符串以空终止符结尾。 嗨,当我用gcc-Wall编译时。我得到这些错误。有人能解释一下我如何修复这些错误吗?谢谢你抽出时间 s_palindrome.c:24:15: warning: variable 'length' is used un
和
库
字符串最多可以包含30个字符。我们必须假设字符串可以包含数字、标点符号和字母。另外,假设字符串中没有空格。必须区分大小写字符。所以“夫人”是回文,但“夫人”不是回文。字符串以空终止符结尾。
嗨,当我用gcc-Wall编译时。我得到这些错误。有人能解释一下我如何修复这些错误吗?谢谢你抽出时间
s_palindrome.c:24:15: warning: variable 'length' is used uninitialized whenever
'for' loop exits because its condition is false
[-Wsometimes-uninitialized]
for (a=0; **a<30**; a++){
is_palindrome.c:36:18: note: uninitialized use occurs here
for (a=0; **a<(length/2)**; a++){
is_palindrome.c:24:15: note: remove the condition if it is always true
for (a=0; **a<30**; a++){
is_palindrome.c:12:18: note: initialize the variable 'length' to silence this
warning
**int a, length;**= 0
s_palindrome.c:24:15:警告:未初始化时使用变量'length'
“for”循环退出,因为其条件为false
[-Wsometimes未初始化]
对于(a=0;**a,存在许多问题,其中大多数问题会阻止程序编译
- 您定义了
string
,但使用了str
everyplace。因此,将定义更改为str
printf(“%s不是回文!”\n);
:换行符在字符串之外,您没有将str
作为参数传递以打印它
printf(“%s”是回文。\n”);
:同样,str
不会传入打印
for(first=0,first=e;str[first]!='\0',first>=0,first++,last--){
- 有两个部分,而不是必需的3,即缺少
;
。它应该在第一个+
之前
- 不能用逗号分隔条件的各个部分。必须使用一些逻辑连接器,如
|
或&&
。但是,在这种情况下,只需要第一部分,因此可以删除第二部分
- 您没有初始化
last
,first=e
应该是last=e
- 修复上述问题的结果:
for(first=0,last=e;str[first]!='\0';first++,last--){
- 在结尾处的
返回之前缺少}
- 不是错误,但是如果(str[first]==temp[first])继续,则可以删除
,因为您仍然处于循环的底部
这是代码
#include <stdio.h>
#include <stdlib.h>
int main (){
char str[30];
int i, len=0;
int flag=1;
printf("Enter a string: \n");
scanf("%s", &str);
for (i=0; str[i]!='\0'; i++){
len++;
}
len--;
for(i=0;i<=len/2;i++)
if(str[i]!=str[len-i])
{
flag=0;
break;
}
if(flag==1)
printf("%s is a palindrome. \n", str);
return 0;
}
#包括
#包括
int main(){
char-str[30];
int i,len=0;
int标志=1;
printf(“输入字符串:\n”);
scanf(“%s”和&str);
对于(i=0;str[i]!='\0';i++){
len++;
}
蓝--;
for(i=0;iint valid=1;//是回文
对于(i=0,j=e;i,这里是您的代码的更正版本,我已经测试过该版本:
#include <stdio.h>
#include <stdlib.h>
int main ()
{
char str[30], temp[30];
int first, last, e;
printf("Enter a string: ");
scanf("%s", str);
for (first=0; str[first]!='\0'; first++)
{
e = first;
}
// loop finds the length of the string, e=first is the index of the last character of the string
// code to store reverse of str in temp
for (first = 0, last = e; str[first] != '\0'; first++, last--)
{
temp[first] = str[last];
}
temp[first] = '\0';
// code to compare str and reverse of str i.e. temp
for (first=0; str[first]!='\0'; first++)
{
// if any character in str and reverse str i.e. temp is unequal, break the loop
if(str[first]!=temp[first])
{
printf("%s is not a palindrome!\n",str);
break;
}
}
// if loop did not break str and reverse str i.e. temp are equal
if(str[first]=='\0')
{
printf("%s is a palindrome.\n",str);
}
return 0;
}
#包括
#包括
int main()
{
char-str[30],temp[30];
int first,last,e;
printf(“输入字符串:”);
scanf(“%s”,str);
for(first=0;str[first]!='\0';first++)
{
e=第一;
}
//循环查找字符串的长度,e=first是字符串最后一个字符的索引
//在temp中存储str反向的代码
for(first=0,last=e;str[first]!='\0';first++,last--)
{
临时[第一次]=str[最后一次];
}
临时[第一]='\0';
//用于比较str和str反转的代码,即temp
for(first=0;str[first]!='\0';first++)
{
//如果str和reverse str中的任何字符(即temp)不相等,请中断循环
如果(str[first]!=temp[first])
{
printf(“%s不是回文!\n”,str);
打破
}
}
//如果循环未中断str和反向str,即温度相等
如果(str[first]='\0')
{
printf(“%s是回文。\n”,str);
}
返回0;
}
有几个问题,例如:
char字符串[30];
已声明,但使用了str
e=first
将不等于字符串的长度,而是比它小一个,这使得它成为字符串最后一个字符的索引
- 此
e
在for中被错误使用(first=0,first=e;str[first]!='\0',first>=0,first++,last--)
,其中,first
初始化两次,使其失去初始值。相反,last
应初始化为e。此外,for
循环可以有多个初始化以及递增/递减(用逗号分隔),但不是多个条件。初始化、条件和递增/递减之间的分隔符应该是分号;
,在您的示例中缺少该分号
- 循环结束时不需要使用
continue;
语句,因为continue
与break
不同,它用于停止当前迭代的进一步执行,并移动到下一个迭代。由于在当前迭代中没有更多的代码要执行,因此continue
是超线程不需要,也不需要
printf
函数使用不正确。在此函数中,您必须提供一个格式说明符作为第一个输入,这是一种如何呈现输出的格式。所有要包含在最终输出中的变量的换行符“\n”、空格和占位符都应在此处指定。resprintf
的t个参数将包含所有变量,占位符已在作为第一个参数传入的格式说明符字符串中定义
- 最后一个
if
缺少大括号
- 我已经纠正了您发布的相同的基本算法,但是,它不是非常有效。更有效的算法可以避免创建另一个反向数组。相反,它将开始比较同一字符串中第一个索引和最后一个索引中的字符,在每一步先递增,最后递减,直到中间的字符被删除达到
#包括`
#包括
内部主(空)
{
字符名[10];
int x,i,r,count=0;
printf(“输入名称:”);
scanf(“%s”、&name);
x=strle
int valid=1;//is palindrome
for(i=0, j=e; i<j; ++i, --j) {
if(str[i] != str[j]){
valid=0;
break;
}
}
#include <stdio.h>
#include <stdlib.h>
int main ()
{
char str[30], temp[30];
int first, last, e;
printf("Enter a string: ");
scanf("%s", str);
for (first=0; str[first]!='\0'; first++)
{
e = first;
}
// loop finds the length of the string, e=first is the index of the last character of the string
// code to store reverse of str in temp
for (first = 0, last = e; str[first] != '\0'; first++, last--)
{
temp[first] = str[last];
}
temp[first] = '\0';
// code to compare str and reverse of str i.e. temp
for (first=0; str[first]!='\0'; first++)
{
// if any character in str and reverse str i.e. temp is unequal, break the loop
if(str[first]!=temp[first])
{
printf("%s is not a palindrome!\n",str);
break;
}
}
// if loop did not break str and reverse str i.e. temp are equal
if(str[first]=='\0')
{
printf("%s is a palindrome.\n",str);
}
return 0;
}
#include<stdio.h>`
#include<string.h>
int main(void)
{
char name[10];
int x,i,r,count = 0;
printf("Enter the Name : ");
scanf("%s",&name);
x = strlen(name);
r = x - 1;
for(i = 0 ; i < x ; i++)
{
if(name[i] == name[r])
{
count = count + 1;
}
else
{
break;
}
r = r - 1;
}
if(count == x)
printf("%s is a Palindrome\n",name);
else
printf("%s is not a Palindrome\n",name);
}