Data structures and algorithms-strings

Source: Internet
Author: User

Question 1: How many words are counted in a character?

Method 1: Use a space as a separator. If a character is measured as a non-space, and the word in front of it is a space, then the number of words "new word begins" is incremented by 1. If the current character is a non-space and the preceding character is also a non-space, it means that the original word continues, and count should not accumulate 1.

Method 2: Use the isstreamstring in Sstream to separate the words, assign the strings to isstreamstring, and separate the words with spaces.

12345678910111213 char string[BUFFERSIZE];int count=0,word=0;while(string[i]!=‘\0‘){    if(c==‘ ‘)        word=0;    else if(word==0)    {        word=1;        count++;    }    i++;}

Question 2: How to reverse a string

Method 1: Normal reverse order, enter another array from the end of the head

Method 2: Reverse in place, point two pointers to the head and tail of the string, use a temporary variable for the head and tail, swap, then the head pointer moves back, and the tail pointer moves forward.

Method 3: Do not allow temporary variables, you can use XOR to point the character before and after the pointer.

while (p>s) {    *p=*p^*s;    *s=*p^*S;    *p=*p^*S;    p--; s++;}     

Question 3: How to find the first occurrence of a character in a string

Method 1: Use the hash table to record the number of occurrences of each character, with the character as the array subscript, and the array size to 256. The first time the scan element will be character statistics, the second pass through to find the first count of 1 characters.

Task 4: All subsets of a string

Reference leetcode:https://leetcode.com/problems/subsets-ii/

Task 5: Finding all permutations of a string

Reference leetcode:https://leetcode.com/submissions/detail/23682769/

Data structures and algorithms-strings

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.