MHB C String Comparison: Debugging Error - "Hello" Output

  • Thread starter Thread starter ksepe
  • Start date Start date
  • Tags Tags
    Comparison String
AI Thread Summary
The discussion centers on a coding error in a C program where the output is incorrectly producing "Hello" instead of "Goodbye" when the input "Quit" is entered. The issue arises from the use of the `strcpy` function in the conditional statement, which does not compare strings but rather copies them. Instead, the correct function to use for comparing strings is `strcmp`, which returns 0 when the two strings are identical. The corrected code snippet provided uses `strcmp` to properly check if `userString` matches "Quit", ensuring that "Goodbye" is printed when the input is correct. Additionally, it is noted that `strcpy` returns a pointer to the destination string, which is not suitable for comparison purposes.
ksepe
Messages
5
Reaction score
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
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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...
Back
Top