Programmer programming Art: Chapter 4: Compile functions similar to strstr/strcpy/strpbrk

Source: Internet
Author: User

Prelude

Some netizens to me, the previous three chapter (http://t.cn/hgVPmH) interview questions, whether it is a bit too difficult. As he said, the interview questions of most companies are not as abnormal or complex as the interview questions of Microsoft and other companies.

The interview examines your knowledge of basic knowledge and whether your programming skills are excellent, or the answer to the questions on the back is much better.

Many small and medium-sized companies have limited creative abilities, including limited human and material resources. Therefore, apart from copying the questions of some large companies (of course, you will certainly not be able to evaluate your knowledge of the basic knowledge. Another way is to allow you to within a limited period of time (such as 10 minutes ), implement some library functions such as strcpy, strcat, and strpbrk on the spot. This mainly depends on your understanding of the details and whether your programming capabilities are solid.

At the same time, the Code in this chapter (except for the c standard library part of the source code in section 4th) is compiled by the individual within a short period of time (exactly, highlighting the sense of presence), many problems, it is inevitable that you will not consider it for weeks. Therefore, if you find any piece of code in this chapter has any problems, please leave it blank.


Section 1: string SEARCH
1.1 Question description:
Given A string A, you must search for A substring B in string.
For example, A = "ABCDF" requires you to search for the substring B = "CD" in ".

Analysis: relatively simple, equivalent to implementing the strstr library function. The main code is as follows:

View plaincopy to clipboardprint?
// Search for the first appearance of the specified string in the string. If the string cannot be found,-1 is returned.
Int strstr (char * string, char * substring)
{
If (string = NULL | substring = NULL)
Return-1;

Int lenstr = strlen (string );
Int lensub = strlen (substring );

If (lenstr <lensub)
Return-1;

Int len = lenstr-lensub;
For (int I = 0; I <= len; I ++) // complexity is O (m * n)
{
For (int j = 0; j <lensub; j ++)
{
If (string [I + j]! = Substring [j])
Break;
}
If (j = lensub)
Return I + 1;
}
Return-1;
}

The above program has implemented the function of searching for the first substring in a string. the time complexity is O (n * m). You can also use the KMP algorithm with the complexity O (m + n ). I will not go into details here.

I hope this crazy series will bring you one way, one way of creativity and one way of thinking, rather than simply providing answers. In that case, everything will always be just a step-by-step, and you and I will not be able to make any progress (but at the same time, it is something that many so-called facial Classics or interview books are very happy to do ).

I think it is different from other interview answers to get people up their minds and improve others' creativity. It is also the significance and value of writing a series of articles.

1.2. Question description

Find the first character that appears only once in a string. If abaccdeff is input, B is output.

The code can be written as follows:

View plaincopy to clipboardprint?
// Find the first character that appears only once,
// Copyright @ yansha
// July, updated, 2011.04.24.
Char FirstNotRepeatChar (char * pString)
{
If (! PString)
Return;

Const int tableSize = 256;
// Note that the space consumed by constants is usually 256. We also think that the space complexity is O (1 ).
Int hashTable [tableSize] = {0}; // store the array and initialize it to 0.

Char * pHashKey = pString;
While (* (pHashKey )! =)
HashTable [* (pHashKey ++)] ++;

While (* pString! =)
{
If (hashTable [* pString] = 1)
Return * pString;

PString ++;
}
Return; // no matching characters found. Exit
}
Code 2, bitmap:

View plaincopy to clipboardprint?
# Include <stdio. h>
# Include <string. h>

Const int N = 26;
Int bit_map [N];

Void findNoRepeat (char * src)
{
Int pos;
Char * str = src;
Int I, len = strlen (src );

// Statistics
For (I = 0; I <len; I ++)
Bit_map [str [I]-a] ++;

// Traverse its bit_map = 1 from the string.
For (I = 0; I <len; I ++)
{
If (bit_map [str [I]-a] = 1)
{
Printf ("% c", str [I]);
Return;
}
}
}

Int main ()
{
Char * src = "abaccdeff ";
FindNoRepeat (src );
Printf ("");
Return 0;
}

Section 2. Copy strings
Description:
Required implementation of the library function strcpy,
Prototype Declaration: extern char * strcpy (char * dest, char * src );
Function: Copies the string ending with NULL indicated by src to the array indicated by dest.
Note: The memory areas specified by src and dest cannot overlap and dest must have sufficient space to accommodate src strings.
Returns the pointer to dest.

Analysis: if the total score of a standard strcpy function is 10, the following are several different answers:
View plaincopy to clipboardprint?
// Scored 2 points
Void strcpy (char * strDest, char * strSrc)
{
While (* strDest ++ = * strSrc ++ )! = );
}

// 4 points
Void strcpy (char * strDest, const char * strSrc)
{
// Add the source string to const, indicating that it is an input parameter and adds 2 points
While (* strDes

Related Article

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.