Implementation of the function of searching the position of a substring in the parent string (C/C ++)

Source: Internet
Author: User
//////////////////////////////////////// ////////////////////////////////
// Title: The function of finding the position of the child string in the parent string (C/C ++)
// Author: z_quan
// Date: 2007/11/16
//////////////////////////////////////// ///////////////////////////////

# Include <string. h>
# Include <stdio. h>

Char * strcasestr (const char * haystack, const char * needle );

Main ()
{
Char str1 [100];
Char str2 [50];
Char * STR;

Do {
Flushall ();
System ("CLS ");

Printf ("enter two strings with a length not greater than 100 and a length not greater than 50 (separated by spaces or carriage return):/N ");

Scanf ("% S % s", str1, str2 );

If (strlen (str1)> 100 | strlen (str2)> 50 ){
Printf ("/n sorry, the string you entered is too long. Please input it again! ");
Continue;
}

STR = strcasestr (const char *) str1, (const char *) str2 );

If (! Str ){
Printf ("the parent string cannot find the matched string with the child string! /N press the Q key to exit. Other Keys continue! /N ");
Continue;
}

Printf ("the first matched substring in the parent string is % d/N", str-str1 + 1 );

Printf ("press the Q key to exit. Other Keys continue! /N ");

} While (getch ()! = 'Q ');
}

/////////// Pointer version of the function to find the position of the substring in the source string ////////////// ///
// Enhance the function after error handling
// 2007/11/16
// 1. Processing is performed when the substring is empty.
// 2. When the substring is longer than the source string
// 3. After each comparison, the pointer of the parent string moves only one digit forward.
// 4. Now we can make a case-insensitive comparison (latest)
// 5. Fixed a bug where the case-insensitive judgment of letters a, a, Z, and Z could not be performed.
// 6. Code the case-insensitive judgment, and the code is obviously less (latest)
// 7. Added the function of clearing all buffers before input and output.
//////////////////////////////////////// ////////////////////////////

/*
////////////////////////////

Function prototype: char * strcasestr (const char * haystack, const char * needle)

Function: points to the start position of the first occurrence of the string needle in the string haystack,
Case Insensitive for string matching. If no string is found, null is returned.

///////////////////////////
*/

Char * strcasestr (const char * haystack, const char * needle)
{
Int I = 0;
While (* haystack & * needle ){
While (* haystack = * needle |/
* Haystack = (* needle> = 'A' & * needle <= 'Z ')? * Needle-32: * needle) |/
* Haystack = (* needle> = 'A' & * needle <= 'Z ')? * Needle + 32: * needle )){
If (! * (++ Needle) Return (char *) Haystack-I;
If (! * (++ Haystack) return 0;
I ++;
}
Needle-= I;
Haystack-= (I-1 );
I = 0;
}
Return 0;
}
//////////////////////////////////////// //////////////////////////////////////
//
//////////////////////////////////////// /////////////////////////////////////////
// A friend's request is accidentally displayed in the group today: a function is required to find the position of the child string where the parent string appears for the first time,
// However, it is required to return the pointer of the character corresponding to that position. I wrote a function to him according to the function header and description.
// It took me five minutes to write a program, but it took me several hours to modify and modify the program,
// I found that I learned a lot. For this function, I wrote several samples and finally decided to use the pointer board.
// In the group, the experts pointed out several bugs at a Glance. After N modifications, they kindly reminded me that I finally changed the program.
// However, there is still a deficiency in the program, that is, there are too many judgments. A friend pointed out to me that the letters in the Child string and the parent string are
// Convert all data into uppercase or lowercase letters before comparison can reduce the time used for judgment, and compare the data with four words at a time.
// I don't know how to improve the running efficiency. Maybe I am too fond of it. Has anyone read this program,
// Find any bugs or methods to improve the efficiency of the exercise. Please tell me. Everyone is a student and can communicate with each other.
//////////////////////////////////////// ////////////////////////////////////////

//////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// /////
//////////// String version comparison function (a little simpler than the above pointer version function :)

Char * strcasestr (const char * haystack, const char * needle)
{
Int I = 0, j = 0;

Int Haylen = strlen (haystack );
Int needlen = strlen (needle );

For (I = 0; I <Haylen ;){

For (j = 0; j <needlen ;){

If (haystack [I] = needle [J])/
| Haystack [I] = (needle [J]> = 'A' & needle [J] <'Z ')? Needle [J]-32: Needle [J])/
| Haystack [I] = (needle [J]> = 'A' & needle [J] <'Z ')? Needle [J] + 32: Needle [J]) {

If (! Needle [++ J]) Return & haystack [I-j + 1];

If (! Haystack [++ I]) return 0;
}
Else {
Break;
}

}
I = I-j + 1;
}

Return 0;
}
//////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// ///
// This function is a non-pointer version of the search location function, it is very difficult to write this program, because when writing it, send it to the group, someone immediately pointed out several bugs.
// Fortunately, after my efforts, the bug has been reduced a lot. Therefore, you can safely use this function. Do not forget to remind you of any bugs.
// Click me.
//
//
//////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// //

Running result (write several data for testing ):

Enter two strings with a length not greater than 100 and a length not greater than 50 (separated by spaces or carriage return ):
11122223333
122
The first matched substring in the parent string is 3.
Press Q to exit. Any other keys continue!

Enter two strings with a length not greater than 100 and a length not greater than 50 (separated by spaces or carriage return ):
Bcdef
De
The first matched substring in the parent string is 3.
Press Q to exit. Any other keys continue!

Enter two strings with a length not greater than 100 and a length not greater than 50 (separated by spaces or carriage return ):
Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa fffffffff

Sorry, the string you entered is too long. Please try again!

Enter two strings with a length not greater than 100 and a length not greater than 50 (separated by spaces or carriage return ):
-=/Abcdef123
/ABCD
The first matched substring in the parent string is 3.
Press Q to exit. Any other keys continue!

Enter two strings with a length not greater than 100 and a length not greater than 50 (separated by spaces or carriage return ):
SDF
Bytes
AAAAAAAAAAAAAAAAAA

Sorry, the string you entered is too long. Please try again!

Enter two strings with a length not greater than 100 and a length not greater than 50 (separated by spaces or carriage return)
D
Aaa
The parent string cannot match the child string!
Press Q to exit. Any other keys continue!

Enter two strings with a length not greater than 100 and a length not greater than 50 (separated by spaces or carriage return ):
A
A
The first matched substring in the parent string is: 1
Press Q to exit. Any other keys continue!

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.