Deletes a specified character from a string.

Source: Internet
Author: User
Tags first string

Http://bidlcy514.blog.163.com/blog/static/105491943201062693329886/

 

Some time ago, when I went to Hulu for a written test, I came up with such a question. I thought it was very simple. If I used it directly, I would delete it directly, and all the subsequent elements moved forward in order, the efficiency is too low and there is no innovation. The problem is divided into two steps. One is to find the characters and the other is to delete the characters. These two methods are simple and seemingly common questions, indeed, Microsoft's interview questions

Question: enter two strings to remove all the characters from the first string. For example, if you enter "they are students." and "aeiou", the first string after deletion becomes "Thy R stdnts .".

 

Analysis: This is a Microsoft interview question. In Microsoft's Frequently Asked Questions, string-related questions account for a large part, because writing program operation strings can well reflect our basic programming skills.

 

Programming functions required to complete this question may not be difficult. After all, the basic idea of this question is to get one in the first string.
In the second string to see if it is in the second string. If yes, it will be deleted from the first string. But how to optimize the efficiency to a satisfactory level is not
An easy task. That is to say, how to delete a character from the first string and how to find a character in the second string requires some tips.

 

First, we will consider how to delete a character from a string. Because the string memory allocation method is continuous. From Words
To delete a character from the string, you must move all the subsequent characters one byte forward. However, if you need to move the characters after the string to be deleted each time, for a string with a length of N, delete
The time complexity of a single character is O (n ). For this question, it is possible that the number of characters to be deleted is N, so the time complexity of this method for deletion is O (n2 ).

 

In fact, we do not need to move all the subsequent characters every time we delete a character. We can imagine that
When a character needs to be deleted, we fill in the position occupied by the character after it, which is equivalent to the deleted character. In specific implementation, we can define two pointers (pfast and
Pslow), initially pointing to the starting position of the first character. When pfast points to a character that needs to be deleted, pfast is skipped and points to the next character. If pfast indicates
If the forward character is a character that does not need to be deleted, assign the pfast character to the character pointed to by pslow, and move both pfast and pstart to the next character at the same time. In this way,
The character skipped by pfast is deleted. With this method, the entire deletion can be completed within O (n) time.

 

Next we will consider how to find a character in a string. Of course, the easiest way is to scan the entire string from start to end. Obviously, this method requires a loop. For a string with a length of N, the time complexity is O (n ).

 

The total number of characters is limited. For eight-character char characters, the total length is only 28 = 256 characters. We
You can create an array with a size of 256 and initialize all elements to 0. Then, for each character in the string, map its ASCII code to an index and set the element corresponding to the index in the array
1. At this time, it becomes faster to search for a character: Based on the ASCII code of the character, find the element in the corresponding subscript of the array. If it is 0, it indicates that the character is not in the string, otherwise, the string
. In this case, the time complexity of searching for a character is O (1 ). In fact, this array is a hash table. For details about this idea, see the 13th questions in this interview series.

 

# Include <stdio. h>

# Include <conio. h>

 

Char thearray [256];

 

Void initthearray (const char * szfind)

{

While ('/0 '! = * Szfind)

Thearray [* szfind ++] = 1;

}

 

Void processthestring (char * szdestination)

{

Char * pfast;

Char * pslow;

 

Pfast = pslow = szdestination;

 

While ('/0 '! = * Pfast)

{

If (0 = thearray [* pfast])

* Pslow ++ = * pfast ++;

Else

Pfast ++;

}

 

* Pslow = '/0 ';

}

 

Int main ()

{

Char szdes [] = "they are students .";

Char szfind [] = "aeiou ";

 

Initthearray (szfind );

Processthestring (szdes );

 

Printf ("% s", szdes );

 

_ Getch ();

Return 0;

}

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.