Examples of solving tasks with character strings. Part 2
Contents
- 1. Function ChangeCharToStr(). Replacing a character in a string
- 2. Function ChangeCharToStrPair(). Replacing a character with another character in a string at paired positions
- 3. Function DelCharAtPos(). Removing a letter from a word given by position number
- 4. Function DelChar(). Remove all occurrences of a given character from a string
- 5. Function DelSubstring(). Remove all occurrences of a specified substring in a string
- 6. Function FormArrayWords(). Forming an array of the frequency of occurrences of words in a string
- Related topics
Search other websites:
1. Function ChangeCharToStr(). Replacing a character in a string
# Replacing a character in a string. # Parameters: # - s - source string; # - c_old - character to be replaced; # - c_new - the character that replaces the c_old character. # The function returns a new (modified) string. def ChangeCharToStr(s, c_old, c_new): # Initial assignments s2 = "" i = 0 # Symbol replacement cycle while i<len(s): if s[i]==c_old: s2 = s2+c_new else: s2 = s2+s[i] i = i+1 return s2 # Demonstration of using the method s = "asd sdks slkdj sss" s2 = ChangeCharToStr(s, 's', 'X') print("s = ", s) print("s2 = ", s2)
Program result
s = asd sdks slkdj sss s2 = aXd XdkX Xlkdj XXX
⇑
2. Function ChangeCharToStrPair(). Replacing a character with another character in a string at paired positions
The ChangeCharToStrPair() function has a code similar to the previous ChangeCharToStr() function. To determine if a position is even, the condition is used
i%2 == 0
where i – position number.
# Replacing a character with another character in a paired position. # Positions 0, 2, 4, ... are considered paired positions. # Parameters: # - s - source string; # - c_old - character to be replaced; # - c_new - the character that replaces the c_old character. # The function returns a new (modified) string. def ChangeCharToStrPair(s, c_old, c_new): # Initial assignments s2 = "" i = 0 # The cycle of replacing characters in paired positions while i<len(s): if (s[i]==c_old)and(i%2==0): s2 = s2+c_new else: s2 = s2+s[i] i = i+1 return s2 # Demonstration of using the method s = "sad sdks slkdj sss" s2 = ChangeCharToStrPair(s, 's', 'X') print("s = ", s) print("s2 = ", s2)
Program result
s = sad sdks slkdj sss s2 = Xad Xdks slkdj sXs
⇑
3. Function DelCharAtPos(). Removing a letter from a word given by position number
To remove a letter from a word at a given position, a slice is used.
# Removing a letter from a word specified by position number # Parameters: # - s - строка, из которой удаляется символ; # - pos - позиция символа в удаляемой строке. def DelCharAtPos(s, pos): # 1. Checking if the data is correct if (pos<0) or (pos>=len(s)): return s # 2. Removing a character through a slice return s[:pos]+s[pos+1:] s = "0123456789" s2 = DelCharAtPos(s, 0) print("s = ", s) print("s2 = ", s2)
The result of the program
s = 0123456789 s2 = 123456789
⇑
4. Function DelChar(). Remove all occurrences of a given character from a string
The DelChar() function creates a new string s2 from the original string s, which does not contain the specified character c.
# Remove all occurrences of a given character from a string # Parameters: # - s - the string to remove the character from; # - c - a character to be deleted. def DelChar(s, c): # newly created string s2 = "" for sym in s: if sym!=c: # all symbols except sym are appended to string s2 s2 += sym return s2 s = "012345363738494" s2 = DelChar(s, '3') print("s = ", s) print("s2 = ", s2)
Program result
s = 012345363738494 s2 = 01245678494
⇑
5. Function DelSubstring(). Remove all occurrences of a specified substring in a string
A modification of the previous DelChar() function is the DelSubstring() function, which removes all occurrences of a substring from a given string.
# Remove all occurrences of a given substring from a string # Parameters: # - s - the string from which the substring is removed; # - sub - the substring to be removed. def DelSubstring(s, sub): # newly created string s2 = "" index_s = 0 while index_s<len(s): # get substring using slice sub2 = s[index_s : index_s+len(sub)] # checking for equality of the original substring sub with the current sub2 if sub2==sub: index_s = index_s + len(sub) else: s2 = s2+s[index_s] index_s = index_s+1 return s2 s = "12344334343455" s2 = DelSubstring(s, '3') print("s = ", s) print("s2 = ", s2)
Program result
s = 12344334343455 s2 = 124444455
⇑
6. Function FormArrayWords(). Forming an array of the frequency of occurrences of words in a string
The FormArrayWords() function determines the frequency of repeating words in a given string. The function returns a dictionary containing string: count pairs. The separator characters are specified as a parameter of the function in the form of a list.
# The dictionary determines the number of occurrences of a word in a string. # Parameters: # - s - the string in question; # - chars - an array of word separators. def FormArrayWords(s, chars): # 1. Take into account that delimiter characters can be at the beginning of a line index_s = 0 while (index_s<len(s))and(s[index_s] in chars): index_s = index_s+1 # 2. Initial settings prev_index = index_s # word start position A = [] # the list of words # 3. Loop for highlighting words in a line as a list while index_s < len(s): if s[index_s] in chars: # if s[index_s] - delimiter character sub = s[prev_index:index_s] # get substring A = A+[sub] # add substring to array # rewind if multiple separator characters are in a string while (index_s<len(s))and(s[index_s] in chars): index_s = index_s+1 prev_index = index_s else: # if there is no separator character index_s = index_s+1 # checking if the last word if index_s==len(s): sub = s[prev_index:index_s] A = A + [sub] # 4. Formation of a dictionary based on list A D = {} for word in A: if word not in D: D[word] = 1 else: D[word] = D[word]+1 return D # Demonstration of using the method # 1. Исходная строка s = " ,, abc defg hjk, , mm nn, op op mm abc, " # 2. Array of delimiting characters chars = [ ' ', ','] # 3. Formation of the frequency dictionary D = FormArrayWords(s, chars) print("D = ", D)
Program result
D = {'abc': 2, 'defg': 1, 'hjk': 1, 'mm': 2, 'nn': 1, 'op': 2}
⇑
Related topics
- Examples of solving tasks with character strings. Part 1
- Solving tasks for processing integers. Part 1
- Solving tasks for processing integers. Part 2
⇑