简体   繁体   中英

Understanding string arrays in c

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;
  }
}
  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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM