-2

The output of the below psuedocode(its psuedocode because it is not meant to be syntactically correct) produces "computers"

strcpy(s1, "computer");
strcpy(s2, "science");

if(strcmp(s1, s2) < 0)
{
   strcat(s1, s2); 
}
else
{
  strcat(s2, s1);
}
s1[strlen(s1) - 6] = '\0';

I am unclear as to how the result is "computers". I cant wrap my head around the logic of how strcmp() is used in in the if() statements

LiveToLearn
  • 163
  • 1
  • 2
  • 12
  • Use a debugger and inspect the values of variables after each line. Look up the documentation for any function you don't understand. – M.M Apr 26 '17 at 23:42
  • @M.M This question is not for debugging. Its a logic question. I am sorry that I was unclear about that. The code I provided was not meant to be ran, it was meant to just look at and figure out the logic. This was my first question on stackoverflow. Im still learning :) – LiveToLearn Apr 27 '17 at 00:52
  • You should try to solve your question first before posting it , and one way of solving it is to follow in the debugger – M.M Apr 27 '17 at 00:57
  • @M.M I wouldn't have gone out of my way to create an account here and post a question to something that I already didn't try to figure out myself. Why would I debug a piece of incomplete code? Again, im trying to understand the logic, not syntax, not error checking, not debugging. Anyway, someone else helped me out. I got my answer. – LiveToLearn Apr 27 '17 at 01:15
  • "Why would I debug a piece of incomplete code?" - because that's a way to figure out the logic of something you don't understand – M.M Apr 27 '17 at 01:57

1 Answers1

2

Okay, first let's see a working example of the code and then I'll explain how it works:

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

void main() {

  char s1[100], s2[100];

  strcpy(s1, "computer");
  strcpy(s2, "science");

  if(strcmp(s1, s2) < 0)
  {
   strcat(s1, s2); 
  }
  else
  {
   strcat(s2, s1);
  }

  s1[strlen(s1) - 6] = '\0';
  printf("%s",s1);
}

Variable s1 gets set to "computer" and s2 gets set to "science". The first string comparison done with strcmp() evaluates whether s1 is less in value than that of s2. If so, then s1 gets concatenated with s2, so that the new value of s1 would be "computerscience". Otherwise whether the strings have the same value or s2 is greater than s1, s2 gets concatenated to s1 with s2 having the resulting value of "sciencecomputer".

The if-conditional that results in a true result is that "computer" is less than "science" so the value of s1 changes and becomes "computerscience". The value 6 is then subtracted from the length of s1 and at that index which happens to hold the character "c", a null character is set as that element's value. So, now the value of s1 is "computers\0ience". This means that when s1 is passed to printf() that function will print every character until it reaches the null character and then the function ceases to display the null as well as the characters following it.

slevy1
  • 3,797
  • 2
  • 27
  • 33
  • Thank you for your response. So, what makes s1 less than s2? Like I know the 'c' from the first letter of "computer" is decimal number 99 according to the ASCII table and 's' from the first letter in "science" is decimal number 115 according to the ASCII table. is this what strcmp is comparing? Im sorry, if this sounds confusing. Im confused so im finding it difficult to articulate my question properly. – LiveToLearn Apr 27 '17 at 00:27
  • 2
    You're welcome. The comparison is performed looking at each character in each of the two strings and the comparison stops when the characters don't match. Here's a link you may find useful: https://www.techonthenet.com/c_language/standard_library_functions/string_h/strcmp.php that deals with strcmp in C. – slevy1 Apr 27 '17 at 00:34