Deletes repeated characters in a string.

Source: Internet
Author: User

actually, it is to change "123456224437889" to "123456789", set a variable last, always point to the next position of a string without repeated characters, and finally set S [last] to 0, get a C string that meets the requirements

Void del_duplicated (char * s) <br/>{< br/> int n = strlen (s); <br/> If (n <= 1) <br/> return; <br/> int I, j; <br/> int last = 1; // initially, only s [0] are considered to have been processed. <br/> for (I = 1; I <n; I ++) // start from subscript 1 and then scan <br/>{< br/> for (j = 0; j <last; j ++) <br/> {<br/> If (s [I] = s [J]) // compare the uncompared characters with s [0... last-1] comparison <br/> break; // returns the same result. If a duplicate occurs, the loop is exited. <br/>}< br/> If (j = last) // This indicates that s [I] = s [J] until last-1. <br/> S [last ++] = s [I]; // Therefore S [I] is a character that has not yet appeared. Add it, and then last + 1 <br/>}< br/> S [last] = 0; // remember that the string should end with 0 <br/>}</P> <p> int main (void) <br/> {<br/> char s [] = "123456? &@? 11173289 "; <br/> del_duplicated (s); <br/> printf (" % s/n ", S); <br/> return 0; <br/>}

as you can see, if the string is composed of the same character, Every time s [I] = s [J] is set up, break is dropped, and J has never changed, all are 0, and only N comparisons are required. If all the characters are different, each last will be added with 1, the number of comparisons is 1 + 2 + 3 +... + n-1 = N (n-1)/2 times.

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.