Search functions in C Language

Source: Internet
Author: User
Tags crypt key string random seed
Search functions
 



Crypt (password or data encoding)
Related functions Getpass
Header file # DEFINE _ xopen_source
# Include <unistd. h>
Define functions Char * crypt (const char * Key, const char * salt );
Function Description Crypt () uses the Data Encryption Standard (DES) algorithm to encode the string referred to by the parameter key. The length of the key string is only the first 8 characters. It is meaningless to exceed the length. The salt parameter is a string consisting of a-Z, a-Z, 0-9, ". "and"/"are used to determine which of the 4096 different built-in tables to use. After the function is successfully executed, a pointer to the encoded string is returned, and the string referred to by the parameter key is not changed. The encoded string is 13 characters long and the first two characters are strings represented by the salt parameter.
Return Value Returns a password string that points to the end of null.
Additional instructions -Lcrypt must be added during GCC compilation.
Example # Include <unistd. h>
Main ()
{
Char passwd [13];
Char * key;
Char slat [2];
Key = getpass ("input first Password :");
Slat [0] = Key [0];
Slat [1] = Key [1];
Strcpy (passwd, crypt (Key slat ));
Key = getpass ("Input second password :");
Slat [0] = passwd [0];
Slat [1] = passwd [1];
Printf ("After crypt (), 1st passwd: % s/n", passwd );
Printf ("After crypt (), 2nd passwd: % s/n", crypt (Key slat ));
}
Run Input first Password:/* enter test, which is encoded and stored in passwd [] */
Input second password/* enter test, and the password will be the same after the same encoding */
After crypt () 1st passwd: teh0wlipw0gyq
After crypt () 2nd passwd: teh0wlipw0gyq
 



Bsearch)
Related functions Qsort
Header file # Include <stdlib. h>
Define functions Void * bsearch (const void * Key, const void * base, size_t nmemb, size_tsize, INT (* COMPAR) (const void *, const void *));
Function Description Bsearch () uses binary search to find data from sorted arrays. The parameter key points to the key data to be searched. The parameter base points to the address starting with the array to be searched. The parameter nmemb indicates the number of elements in the array. The size of each element is determined by the parameter size, the last COMPAR parameter is a function pointer used to determine the size relationship between two elements, if the element data referred to by the first parameter passed to COMPAR is greater than the element data referred to by the second parameter, the value greater than 0 must be returned. If the two elements are equal, the return value is 0.
Additional instructions If key data is found, the address is returned. If no key data is found in the array, null is returned.
Example # Include <stdio. h>
# Include <stdlib. h>
# Define nmemb 5
# Define size 10
Int COMPAR (const void * a, const void * B)
{
Return (strcmp (char *) A, (char *) B ));
}
Main ()
{
Char data [50] [size] = {"Linux", "FreeBSD", "Solaris", "SunOS", "windows "};
Char key [80], * base, * offset;
Int I, nmemb = nmemb, size = size;
While (1 ){
Printf ("> ");
Fgets (Key, sizeof (key), stdin );
Key [strlen (key)-1] = '/0 ';
If (! Strcmp (Key, "exit") break;
If (! Strcmp (Key, "list ")){
For (I = 0; I <nmemb; I ++)
Printf ("% s/n", data [I]);
Continue;
}
Base = data [0];
Qsort (base, nmemb, size, COMPAR );
Offset = (char *) bsearch (Key, base, nmemb, size, COMPAR );
If (offset = NULL ){
Printf ("% s not found! /N ", key );
Strcpy (data [nmemb ++], key );
Printf ("Add % s to data array/N", key );
} Else {
Printf ("found: % s/n", offset );
}
}
}
Run > Hello/* enter the hello string */
Hello not found! /* The Hello string cannot be found */
Add hello to data array/* Add the hello string */
>. List/* list all data */
FreeBSD
Linux
Solaris
SunOS
Windows
Hello
> Hello
Found: Hello
 



Lfind (linear search)
Related functions Lsearch
Header file # Include <stdlib. h>
Define functions Void * lfind (const void * Key, const void * base, size_t * nmemb, size_t
Size, INT (* COMPAR) (const void *, const void *));
Function Description Lfind () uses linear search to search for data from the beginning to the end of an array. The parameter key points to the key data to be searched. The parameter base points to the address starting with the array to be searched. The parameter nmemb indicates the number of elements in the array. The size of each element is determined by the parameter size, the last COMPAR parameter is a function pointer, which is used to determine whether two elements are the same, if the element data referred to by the remote parameter passed to COMPAR is the same as the element data referred to by the second parameter, 0 is returned. If the two elements are different, a non-0 value is returned. The difference between lfind () and lsearch () is that when key data cannot be found, lfind () Only returns NULL, rather than actively adding the data to the end of the array.
Return Value If the key data is found, the address of the element is returned. If the key data cannot be found in the array, a null pointer is returned ).
Example Refer to lsearch ().
 



Lsearch)
Related functions Lfind
Header file # Include <stdlib. h>
Define functions Void * lsearch (const void * Key, const void * base, size_t * nmemb, size_t size, INT (* COMPAR) (const void *, const void *));
Function Description Lsearch () uses linear search to search for data from the beginning to the end of an array. The parameter key points to the key data to be searched. The parameter base points to the address starting with the array to be searched. The parameter nmemb indicates the number of elements in the array. The size of each element is determined by the parameter size, the last COMPAR parameter is a function pointer, which is used to determine whether two elements are the same, if the element data referred to by the first parameter passed to COMPAR is the same as the element data referred to by the second parameter, 0 is returned. If the two elements are different, a non-0 value is returned. If the key data cannot be found by lsearch (), the data is automatically added to the array.
Return Value If the key data is found, the limbs of the element are returned. If the key data cannot be found in the array, the key data is added to the array, and the address after the array is returned.
Example # Include <stdio. h>
# Include <stdlib. h>
# Define nmemb 50
# Define size 10
Int COMPAR (comst void * a, const void * B)
{
Return (strcmp (char *) A, (char *) B ));
}
Main ()
{
Char data [nmemb] [size] = {"Linux", "FreeBSD", "solzris", "SunOS", "windows "};
Char key [80], * base, * offset;
Int I, nmemb = nmemb, size = size;
For (I = 1; I <5; I ++ ){
Fgets (Key, sizeof9key), stdin );
Key [strlen (key)-1] = '/0 ';
Base = data [0];
Offset = (char *) lfind (Key, base, & nmemb, size, COMPAR );
If (offset = NULL ){
Printf ("% s not found! /N ", key );
Offset = (char *) lsearch (Key, base, & nmemb, size, COMPAR );
Printf ("Add % s to data array/N", offset );
} Else {
Printf ("found: % s/n", offset );
}
}
}
Run Linux
Found: Linux
OS/2
OS/2 not found!
Add OS/2 to data array
OS/2
Found: OS/2
 



Qsort (sort arrays by quick sorting)
Related functions Bsearch
Header file # Include <stdlib. h>
Define functions Void qsort (void * base, size_t nmemb, size_t size, INT (* COMPAR) (const void *, const void *));
Function Description The base parameter points to the address starting with the array to be sorted. The nmemb parameter indicates the number of elements in the array. The size of each element is determined by the parameter size. The final parameter COMPAR is a function pointer, this function is used to determine the size relationship between two elements. If the element data passed to the first parameter of COMPAR is greater than the element data referred to by the second parameter, it must return a value greater than zero, if the two elements are equal, 0 is returned.
Return Value
Additional instructions
Example # Define nmemb 7
# Include <stdlib. h>
Int COMPAR (const void * a, const void * B)
{
Int * AA = (int *) A, * BB = (int *) B;
If (* AA> * bb) return 1;
If (* AA = * bb) return 0;
If (* AA <* bb) Return-1;
}
Main ()
{
Int base [nmemb] = {3,102, 5 };
Int I;
For (I = 0; I <nmemb; I ++)
Printf ("% d", base [I]);
Printf ("/N ");
Qsort (base, nmemb, sizeof (INT), COMPAR );
For (I = 0; I <nmemb; I ++)
Printf ("% d" base [I]);
Printf ("/N ");
}
Run 3 102 5-2 98 52 18
-2 3 5 18 52 98 102
 



Rand (Random Number Generation)
Related functions Srand, random, srandom
Header file # Include <stdlib. h>
Define functions Int rand (void)
Function Description Rand () returns a random value ranging from 0 to rand_max. Before calling this function to generate a random number, you must use srand () to set the random number seed. If no random number seed is set, Rand () will automatically set the random number seed to 1. For details about random seed, see srand ().
Return Value Returns a random value between 0 and rand_max. rand_max is defined in stdlib. h and its value is 2147483647.
Example /* Generate a random value ranging from 1 to 10. In this example, no random seed is set. For the complete random number generation, see
Srand ()*/
# Include <stdlib. h>
Main ()
{
Int I, J;
For (I = 0; I <10; I ++)
{
J = 1 + (INT) (10.0 * rand ()/(rand_max + 1.0 ));
Printf ("% d", J );
}
}
Run 9 4 8 8 10 2 4 8 3 6
9 4 8 8 10 2 4 8 3 6
 



Srand (set Random Seed)
Related functions Rand, random srandom
Header file # Include <stdlib. h>
Define functions Void srand (unsigned int seed );
Function Description Srand () is used to set the random number seed when rand () generates a random number. The seed parameter must be an integer. Generally, the return value of geypid () or time (0) can be used as seed. If the same value is set for each seed, the random values generated by rand () are the same each time.
Return Value
Example /* Generate a random number ranging from 1 to 10. This example and execution result can be referenced with RAND */
# Include <time. h>
# Include <stdlib. h>
Main ()
{
Int I, J;
Srand (INT) time (0 ));
For (I = 0; I <10; I ++)
{
J = 1 + (INT) (10.0 * rand ()/(rand_max + 1.0 ));
Printf ("% d", J );
}
}
Run 5 8 8 8 10 2 10 8 9 9
2 9 7 4 10 3 2 10 8 7

 

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.