Solved

Questions on strcmp and strcpy in C

Posted on 2009-04-18
342 Views
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) ;
``````
0
Question by:clscor2

LVL 41

Accepted Solution

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 */
``````
0

LVL 41

Expert Comment

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

LVL 39

Expert Comment

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

LVL 39

Expert Comment

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

LVL 39

Expert Comment

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

LVL 41

Expert Comment

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

LVL 39

Expert Comment

oops... thanks for spotting that :)
0

Author Closing Comment

Thank you!
0

Featured Post

Suggested Solutions

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Read about how to choose the best possible content marketing agency to suit your needs. Content marketing has become an integral part of running a successful tech business, so it is wise to be informed.
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.