简体   繁体   中英

swap strings and check unequal condition in C?

#include <stdio.h>
#define MAX 100
void swap_string1(char *A, char *B);

int main (void) {
  char S1[MAX]="ABC";   //tried these with different
  char S2[MAX]="defgh"; //strings for S1 and S2
  printf("S1: %s\nS2: %s\n",S1,S2);
  swap_string1(S1,S2);
  printf("S1: %s\nS2: %s\n",S1,S2);
  swap_string1(S1,S2);
  printf("S1: %s\nS2: %s\n",S1,S2);

  return 0;
}
void swap_string1(char *A, char *B) {
  char temp, *tempP;
  while ( *A != '\0' && *B != '\0' ) {
    temp = *A;
    *A = *B;
    *B = temp;
    A++ ; B++ ;
  }
  //strings unequal lengths, so finish-off the tail end
  if ( *A == '\0' && *B !='\0' ) {
     tempP=B;
     while ( *B ) *A++=*B++;
     *A='\0' ; *tempP='\0' ;
  }
  else if ( *A != '\0' && *B =='\0' ) {
     tempP=A;
     while ( *A ) *B++=*A++;
     *B='\0' ; *tempP='\0' ;
  }
}

Here is the code I get from notes which can swap two strings by pointer, but I don't really undestand the part how to check the string with unequal length.

Can anyone explain that part to me?

Its pretty simple:

*A and *B (value referenced by pointer) refer to the character value of A and B in if ( *A == '\\0' && *B !='\\0' ) or if ( *A != '\\0' && *B =='\\0' ) and they run uptil their respective current character reaches the terminating character (also referred to as end of line or eof especially why dealing with files). since there is a '&&' operator it means that the stuff inside loop occurs until the string with the smaller number (considering they are unequal) of characters reaches eof or '\\0'.

If the strings were to be equal then they would both reach their terminating character together which is the first while loop.

Swap part: (for the if part of being strings being unequal)

Going by your code firstly there is a temporary pointer tempP which is initialized to the address of B before entering the while loop while(*B) for holding B's pointed location. We assign it since we will need to collect value of B again (doing in a temporary pointer) with the eof character. (The value changes when you do *A++=*B++ because its a referenced arithmetic - similar to call by reference for functions which changes the value of the passed parameters)

Now since we need to swap the remaining part (the characters after the equal number of characters in A and B), we assign the value of string B to string A inside the while loop by *A++=*B++ ('++' to iterate on the values, to go through character by character in A and B. For pointer arithmetic it would be memory addresses like +4 each for the integer, but its the value we are referencing here) and you can notice the loop condition is while(*B) which is the same as writing while(*B!='\\0') , it means the loop runs till the last character of B (right before eof character). after swapping that eof character isn't touched or brought into place for string A (with string B values now), hence we assign *A='\\0' . Similarly to terminate string B we require *B='\\0' which is *tempP='\\0' here. (since we assigned temporary pointer tempP to B's address, and B value changed by *A++=*B++ )

Similarly the other way round for the else part.

if ( *A == '\\0' && *B !='\\0' ) { // if B is longer
tempP=B; // tempP=B; // remember where the copy should end
while ( *B ) *A++=*B++; // while ( *B ) *A++=*B++; // copy the rest of B into A
*A='\\0' ; *tempP='\\0' ; // *A='\\0' ; *tempP='\\0' ; // terminate A and B
}
else if ( *A != '\\0' && *B =='\\0' ) { // if A is longer
tempP=A; // tempP=A; // remember where the copy should end
while ( *A ) *B++=*A++; // while ( *A ) *B++=*A++; // copy the rest of A into B
*B='\\0' ; *tempP='\\0' ; // *B='\\0' ; *tempP='\\0' ; // terminate B and A
}

  1. To swap strings, you can simply swap the two pointers.

  2. To determine equality of the two strings, you can simply use the build-in function strcmp(str1, str2) after #include <string.h> .

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