在c中比较两个具有相同名称的不同字符串
Comparing two different strings with the same name in c
我现在正在学习如何用 c 编写代码,因此,我的下一步计划是学习如何递归编写代码。为此,我尝试编写接受用户输入的代码,然后递归地反转它并告诉你它是否是 palindrome
。
当前问题第 3 个,第一个是我是否一直在递归编写,第二个是我必须在这部分代码中进行哪些字符串比较以确定它是否是 palindrome
与否:
int main(){
//char stringran[256];
//char done;
char str1[1024];
int size;
printf("Enter a string to reverse, or done to exit program: ");
scanf("%s", str1);
size = strlen(str1);
printf("The string you enterred is: %s\n", str1);
reverse(str1, 0, size - 1);
printf("The string after reversing is: %s\n", str1);
//the if-else statement below is the issue, currently i have a placeholder//
if(str1 == str1){
printf("The string is a palindrome");
}
else{
printf("The string is not a palindrome");
}
最后,如果我希望循环代码以便它在输入字符串后不断询问初始问题(输入字符串以反转,或完成以退出程序),我将如何去做?是 for 循环还是 while 循环?
带输出的完整代码:
"The current issues number 3"
- "The first is whether or not I have even been writing recursively."
是的,您的 reverse()
函数是递归的。任何调用自身的函数都是递归的。但是,很容易编写出运行不正确、不能很好地处理适当情况、破坏内存管理或无限运行的递归函数。在 C
中编程需要非常小心;写递归函数更要小心
如评论中所述,检测回文 不需要 需要递归函数。作为一个练习,我想这没问题,但是(1)如果你真的遇到这个问题,你最好换一种方式来解决它,并且(2)学习递归有更好的问题,因为既简单又更适合递归方法。 Google 是你的朋友。
- "which string comparison I have to do in this part of the code to determine if its a palindrome or not"
您需要做的主要事情是比较可能不同的两件事。正如评论指出的那样 str1 == str1
总是 true
。您指出这是占位符代码(以便编译)。更好的占位符代码是:
if (1) { // placeholder code so that it compiles
那会消除很多混乱。
至于你需要做的对比,在修改之前复制str1
即可。然后,将修改前的副本与修改后的值进行比较。但是当你复制 str1
时,一定要知道你在做什么。由于您还不清楚需要执行此操作,因此您可能不太清楚 如何 执行此操作。这是 C
的陷阱之一,很容易弄错。同样,Google 会在这里帮助你。
- "if I wish to loop the code so that it keeps asking the initial question after a string is input, how would I go about doing it? Would it be a for-loop or a while-loop?"
两者都可以,因为编写一个像 while
循环一样的 for
循环是微不足道的。真正的问题是,在什么情况下你会跳出循环?该问题的答案将为您指明最佳循环类型和循环条件。
我现在正在学习如何用 c 编写代码,因此,我的下一步计划是学习如何递归编写代码。为此,我尝试编写接受用户输入的代码,然后递归地反转它并告诉你它是否是 palindrome
。
当前问题第 3 个,第一个是我是否一直在递归编写,第二个是我必须在这部分代码中进行哪些字符串比较以确定它是否是 palindrome
与否:
int main(){
//char stringran[256];
//char done;
char str1[1024];
int size;
printf("Enter a string to reverse, or done to exit program: ");
scanf("%s", str1);
size = strlen(str1);
printf("The string you enterred is: %s\n", str1);
reverse(str1, 0, size - 1);
printf("The string after reversing is: %s\n", str1);
//the if-else statement below is the issue, currently i have a placeholder//
if(str1 == str1){
printf("The string is a palindrome");
}
else{
printf("The string is not a palindrome");
}
最后,如果我希望循环代码以便它在输入字符串后不断询问初始问题(输入字符串以反转,或完成以退出程序),我将如何去做?是 for 循环还是 while 循环?
带输出的完整代码:
"The current issues number 3"
- "The first is whether or not I have even been writing recursively."
是的,您的 reverse()
函数是递归的。任何调用自身的函数都是递归的。但是,很容易编写出运行不正确、不能很好地处理适当情况、破坏内存管理或无限运行的递归函数。在 C
中编程需要非常小心;写递归函数更要小心
如评论中所述,检测回文 不需要 需要递归函数。作为一个练习,我想这没问题,但是(1)如果你真的遇到这个问题,你最好换一种方式来解决它,并且(2)学习递归有更好的问题,因为既简单又更适合递归方法。 Google 是你的朋友。
- "which string comparison I have to do in this part of the code to determine if its a palindrome or not"
您需要做的主要事情是比较可能不同的两件事。正如评论指出的那样 str1 == str1
总是 true
。您指出这是占位符代码(以便编译)。更好的占位符代码是:
if (1) { // placeholder code so that it compiles
那会消除很多混乱。
至于你需要做的对比,在修改之前复制str1
即可。然后,将修改前的副本与修改后的值进行比较。但是当你复制 str1
时,一定要知道你在做什么。由于您还不清楚需要执行此操作,因此您可能不太清楚 如何 执行此操作。这是 C
的陷阱之一,很容易弄错。同样,Google 会在这里帮助你。
- "if I wish to loop the code so that it keeps asking the initial question after a string is input, how would I go about doing it? Would it be a for-loop or a while-loop?"
两者都可以,因为编写一个像 while
循环一样的 for
循环是微不足道的。真正的问题是,在什么情况下你会跳出循环?该问题的答案将为您指明最佳循环类型和循环条件。