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