• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 359
  • Last Modified:

Questions on strcmp and strcpy in C

Can anyone explain to me what each of these lines is doing?

Thank you@
for (i = last ; i > 0 ; i--) /* begin sorting */
         for (j = 1 ; j <= i ; j++)
            if (strcmp(name[j],name[j - 1]) < 0) {
               strcpy(hold,name[j]) ;
               strcpy(name[j],name[j - 1]) ;
               strcpy(name[j - 1],hold) ;

Open in new window

0
clscor2
Asked:
clscor2
  • 4
  • 3
1 Solution
 
pcelbaCommented:
Hope this description is accurate enough. I haven't write C code for years...
for (i = last ; i > 0 ; i--) /* begin sorting - loop starts on the last element in name and goes to the first one */
         for (j = 1 ; j <= i ; j++) /* loop starts from the first element and goes to the one given by above loop */
            if (strcmp(name[j],name[j - 1]) < 0) {   /* If the right string is greater than the left one swap them */
               strcpy(hold,name[j]) ;  /* remember the left string  */
               strcpy(name[j],name[j - 1]) ;  /* copy right string to the left one */
               strcpy(name[j - 1],hold) ;  /* copy the remembered string to the right one */
 
/* If the string are in right order no swapping is done */
/* this process also called bubble sort continues until all names are sorted */
/* each pass through the "for (j = 1 ; ..." loop moves the highest name to the end of the list, then the list size is decreased (i--) and sorting continues with the second highest element  */
/* this algorithm is not optimized, it could finish if there is no swap in j-loop */

Open in new window

0
 
pcelbaCommented:
Some terminology correction -
"for (j = 1 ; j <= i ; j++) /* loop starts from the first element..."  is not accurate because the first element has index [0], so the loop starts with the second element [j=1] and compares it to the first element in the first pass.
0
 
evilrixSenior Software Engineer (Avast)Commented:
The code appears to be performing a sort on the array 'name'.

Line 1 is an "outer loop" that counts back from last (the last entry in name?) to 1
Line 2 is an "inner" loop that counts from 1 up to the current value of i
Line 3 compares the value of the element indexed by j with the one before it and if it is less than it then
Line 4 the element at position j is put into a hold variable
Line 5 the element in name at position j-1 is swapped with the element at position j
Line 6 the element in hold is put into the position j-1 of name

the inner loop increments j and repeats as long as j is greater-than or equal to i
the outer loop decrement i and repeats as long as i is greater-than 0

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
evilrixSenior Software Engineer (Avast)Commented:
BTW: In the interests of simplicity I have used the term 'element' in a generic sense; clearly the elements contains string values and it is these that are being compared and swapped as necessary.
0
 
evilrixSenior Software Engineer (Avast)Commented:
The Wikipedia page on bubble sorts will give you more insight into what's happening here and has a pretty cool animation that might help you visualise what the code is doing. There are also some links to other pages that provide additional useful info.

http://en.wikipedia.org/wiki/Bubble_sort

0
 
pcelbaCommented:
One small correction:

"the inner loop increments j and repeats as long as j is greater-than or equal to i"

should change to:

"the inner loop increments j and repeats as long as j is LESS-than or equal to i"
0
 
evilrixSenior Software Engineer (Avast)Commented:
oops... thanks for spotting that :)
0
 
clscor2Author Commented:
Thank you!
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now