C String Comparison: Debugging Error - "Hello" Output

In summary, the conversation discusses an issue where a computer generated word is not producing the expected output "Goodbye" when the input "Quit" is entered. The solution is to use the function strcmp(const char *str1, const char *str2) to compare the input string with the desired output, and only print "Goodbye" if the two strings are identical. Additionally, it is mentioned that the function strcpy(char *destination, char *source) returns a pointer to the destination string if the operation is successful.
  • #1
ksepe
5
0
I am trying to compare a computer generated word to display the following however, when Quit is entered it produces the output "Hello". What is my error in the code?

Code:
#include <stdio.h>
#include <string.h>

int main(void) {
   char userString[50];

   strcpy(userString, "Quit");

   /* Your solution goes here  */
if (strcpy(userString,"Quit")==0){
   printf("Goodbye\n");
}
else {
   printf("Hello\n");
}
   return 0;
}
 
Last edited by a moderator:
Technology news on Phys.org
  • #2
On an ANSI-compliant system, strcmp(const char *str1, const char *str2) returns 0 if the strings str1 and str2 are identical.

Try

Code:
#include <stdio.h>
#include <string.h>

int main(void) {
		char userString[50];

		strcpy(userString, "Quit");

		/* Your solution goes here */

		if (strcmp(userString, "Quit") == 0){
			printf("Goodbye\n");
		}
		else {
			printf("Hello\n");
		}

		return 0;
}

The function strcpy(char *destination, char *source) returns a pointer to the destination string: a non-zero value.

*Note: you may post formatted source code by enclosing the source code in [code]...[/code] tags.
 

Related to C String Comparison: Debugging Error - "Hello" Output

What is a C string comparison?

A C string comparison is a method of comparing two strings of characters in the C programming language. It is used to determine if two strings are equal or if one comes before or after the other in alphabetical order.

What is a debugging error in the context of C string comparison?

A debugging error in the context of C string comparison refers to an error that occurs when trying to compare two strings. This could be due to incorrect syntax, missing or incorrect arguments, or other issues that prevent the code from running properly.

Why am I getting an "Hello" output when trying to perform a C string comparison?

This output may occur if there is a debugging error in your code. It could also be caused by the strings being compared not being equal, so the code is not entering the correct conditional statement and printing "Hello" instead.

How can I fix a debugging error in C string comparison?

To fix a debugging error in C string comparison, you can use a debugger tool to step through your code and identify where the error is occurring. You can also carefully review your code and check for any syntax or logic errors that may be causing the issue.

What are some common mistakes to avoid when performing a C string comparison?

Some common mistakes to avoid when performing a C string comparison include forgetting to use the proper string comparison function, not properly declaring or initializing the strings to be compared, and not accounting for case sensitivity in the strings.

Similar threads

Back
Top