0

I am trying to understand this particular line of code.

I'm having trouble comprehending why there are 3 assignment statements needed for this. I figure it is the minimum necessary, I just can't seem to follow it with my mind.

If someone could take me through what each line of this does, in english, that would be fantastic.

Thanks.

void to_upper(char *word) {

  int index = 0;

  while (word[index] != '\0') {
    word[index] = toupper(word[index]);
    index++;
  }
}

int length(char *word) {

  int index=0;

  while (word[index] != '\0')
    index++;
  return index;
}

void reverse(char *word) {
  int index, len;
  char temp;
  len = length(word);
  for (index=0; index<len/2; index++) {
    temp = word[index];
    word[index] = word[len-1-index];
    word[len-1-index] = temp;
  }
}
5
  • 5
    That's more than one line of code. Exactly what part are you confused about? Presuming that you're asking about reverse, how would you do it with fewer assignments? Commented Feb 19, 2013 at 2:28
  • 1
    to_upper and length are not really part of what you're asking about, are they? Commented Feb 19, 2013 at 2:28
  • Why do you have a custom length instead of just using strlen? Commented Feb 19, 2013 at 2:29
  • Are you talking about the swapping code? Can you think of a way to swap without the temp variable? (possible in this case but less readable, and error prone..) Commented Feb 19, 2013 at 2:30
  • On the first iteration, the triple assignment swaps the first and last characters; on the second iteration, it swaps the first+1 and last-1 characters; and on subsequent iterations keeps on swapping pairs of characters similarly. Commented Feb 19, 2013 at 2:38

2 Answers 2

2
  for (index=0; index<len/2; index++) {
1    temp = word[index];
2    word[index] = word[len-1-index];
3    word[len-1-index] = temp;
  }

1: store the value of word[index] (we'll need it later)

2: store the value of the word array that is equidistant from the midpoint of the array into word[index]

3: store the original value of word[index] into the position equidistant from the midpoint of the array

e.g.: if index = 0, the first word is exchanged with the last and so on.

Sign up to request clarification or add additional context in comments.

1 Comment

Sorry, I modified the code but not the question, the question was in regards to the last paragraph.
1

I am going to assume you understand the length and to_upper parts of your code as they are basically c++ 101 stuff.

//Well, just be the title, I would assume this function reverses a string, lets continue.
void reverse(char *word) {
  int index, len;  //declares 2 integer variables
  char temp;       //creates a temporary char variable
  len = length(word); //set the length variable to the length of the word
  for (index=0; index<len/2; index++) {
    //Loop through the function, starting at 
    //index 0, going half way through the length of the word
    temp = word[index]; //save the character at the index
    word[index] = word[len-1-index]; //set the character at the index in the array 
                                     //to the reciprocal character.
    word[len-1-index] = temp; //Now set the reciprocal character to the saved one.
  }
}

//This essentially moves the first letter to the end, the 2nd letter to the 2nd 
//to end, etc.

So if you have the word "race" it swaps the "r" with the "e" and then the "a" with the "c" to get a final string of "ecar", or race backwards.

To understand why they need 3 assignments: if you set word[index] = word[len-1-index] then in both places, the same character exists. This would be like setting "race" to "racr". If you then set word[len-1-index] = word[index], you would just be putting the same character back in the first part, so you would go from "racr" to "racr". You need a temporary variable to save the original value so you can replace the character at the front of the string.

2 Comments

It must be C 101, not C++ 101; this is a C question!
Lol, valid. Do they even offer C classes at colleges anymore? They didn't when I went.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.