C Language -- pointer and array (2)

Source: Internet
Author: User

1. Write a function using a pointer to exchange two integer numbers a and B read from the keyboard;

# Include <stdio. h>
# Include <stdlib. h>
Void swap (int * P1, int * P2 ){
Int temp;
Temp = * P1;
* P1 = * P2;
* P2 = temp;
}

Int main ()
{
Int X, Y;
Printf ("enter two numbers: \ n ");
Scanf ("% d", & X );
Scanf ("% d", & Y );
Printf ("two input numbers: % d, % d \ n", x, y );
Swap (& X, & Y );
Printf ("the number of two after the switch is % d, % d \ n", x, y );
}

Here, think: int * P when defined; indicates that p is a pointer to an integer space (that is, an address); in the program, * P indicates taking the content of the space referred to by P; & X indicates the address of X;

Swap (& X, & Y) is the address of X and Y, and the corresponding void swap (int * P1, int * P2) is the address ;, the parameter is a pointer, that is, an address.

2. Compile a void invert (char STR []) function to reverse the content of a string;

# Include <stdio. h>
# Include <stdlib. h>
Void inverse (char STR [])
{
Char * P1 = STR;
Char * P2 = STR;
Char temp;
While (* P2! = '\ 0 ')
P2 ++;
P2 --;
While (P2> P1)
{
Temp = * P1;
* P1 = * P2;
* P2 = temp;
P2 --;
P1 ++;
}
}

Int main ()
{
Char STR [100];
Scanf ("% s", STR );
Inverse (STR );
Printf ("inverse Hou de is % s \ n", STR );

}

3. Compile a function int index (char * s, char * t) to check whether string S contains string T, if the value contains, the start position (lower value) of T in S is returned; otherwise,-1 is returned;

# Include <stdio. h>
# Include <stdlib. h>
Int index (char * s, char * t)
{
Int I, j, n = 0;
For (I = 0; t [I]! = '\ 0'; I ++)
N ++;
For (I = 0; s [I]; I ++)
For (j = 0; s [I + J] = T [J]; j ++)
If (J + 1 = N)
Return I;
Return-1;
}

Int main ()
{
Char s [100];
Char T [100];
Int X;
Printf ("Enter the value of string S: \ n ");
Scanf ("% s", S );
Printf ("Enter the value of string T: \ n ");
Scanf ("% s", t );
X = index (S, T );
Printf ("t position in S: % d \ n", x + 1 );
Return 0;
}

 

The yellow shadow part is the emphasis here. Mark, this is a good way to find whether string 1 contains string 2. First, calculate the length of 2, then compare one of 1 and one of 2,

 

For (I = 0; t [I]! = '\ 0'; I ++)
N ++;
For (I = 0; s [I]; I ++) loop 1
For (j = 0; s [I + J] = T [J]; j ++) loop 2
If (J + 1 = N)
Return I;

For example, if S is ABCDE and T is CD

  I value J value Loop Execution Process: S, T
1 0 0 I = 0, Judge S is not over, j = 0, Judge s [0]! = J [0], loop 2 ends, I = 1; bottom
2 1 0 I = 1, Judge S is not over, j = 0, Judge s [1]! = J [0], loop 2 ends, I = 2; bottom
3 2 0

I = 2, Judge S is not over, j = 0, Judge s [2] = J [0], 0 + 1! = N, j = 1, Judge s [3] = T [1], 1 + 1 = N, return I = 2, the loop ends, and the function ends.

4      
5      

 

4. Write a function to determine whether a string is input. If input is input, the return value is 1. Otherwise, the return value is 0;

# Include <stdio. h>
# Include <stdlib. h>
Int Huiwen (char * Str)
{
Char * P1 = STR, * P2 = STR;
While (* P2! = '\ 0 '){
P2 ++;
}
P2 --;
While (P2> P1)
If (* P1 = * P2)
{
P1 ++;
P2 --;
} Else
Return 0;
If (P1> P2)
Return 1;
Return 0;

}

Int main ()
{
Char STR [100];
Printf ("Enter the STR value \ n ");
Scanf ("% s", STR );
If (Huiwen (STR) = 1)
Printf ("is a reply \ n ");
Else
Printf ("not input \ n ");
Return 0;
}

Similar to the two-question method, in the search for the STR string length, the so-called return is originally a symmetric meaning of the string content. Soga...

5. Compile a function juge (INT (* P) [N], int N) and J to determine whether the nxn matrix is an upper triangle matrix. The upper-Triangle Matrix does not contain the primary and lower-half triangle elements;

# Include <stdio. h>

# Include <stdlib. h>

Int Jude (INT (* P) [100], int N)

{

Int I, J;

For (I = 0; I <n; I ++)

{

For (j = 0; j <I; j ++ ){

If (* (p + I) + J )! = 0)

Return 0;

}

Return 1;

}

}

Int main ()

{

Int N;

Scanf ("% d", & N );

Int * P, A [100] [100];

Int I;

Int J;

For (I = 0; I <n; I ++)

For (j = 0; j <n; j ++)

Scanf ("% d", & A [I] [J]);

Printf ("% d \ n", Jude (A, n ));

}

 

 

6. Compile a function sort (char * STR [], int N) and sort the strings using pointer arrays;

# Include <stdio. h>

# Include <stdlib. h>

# Include <string. h>

Void sort (char * STR [], int N ){

Int I, J;

Char * temp;

For (j = 1; j <n-1; j ++)

For (I = 0; I <n-1-j; I ++)

If (strcmp (STR [I], STR [I + 1])> 0)

{

Temp = STR [I];

STR [I] = STR [I + 1];

STR [I + 1] = temp;

}

 

 

}

Int main ()

{

Int I;

Static char * book [] = {"Visual Basic", "Visual C ++", "Visual FoxPro", "C ",

"Pascal", "Visual J ++ "};

Sort (book, 6 );

For (I = 0; I <6; I ++)

Printf ("% s \ n", Book [I]);

}

 

Strcmp Functions

The general format is strcmp (string 1, string 2)

Strcmp compares string 1 and string 2.

Example: strcmp (str1, str2 );

Strcmp ("China", "Korea ″);

Strcmp (str1, "Beijing ″);

The comparison result is returned by the function value.

(1) If String 1 = string 2, the function value is 0.

(2) If String 1> string 2, the function value is a positive integer.

(3) If String 1 <string 2, the function value is a negative integer.

Note: The two strings cannot be compared in the following format:

If (str1> str2) printf ("yes ″);

And can only be used

If (strcmp (str1, str2)> 0) printf ("yes ″);

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.