- 浏览: 10951 次
- 性别:
- 来自: 体育仓库
最新评论
-
ravenex:
chong_zh 写道例子中的Cindy,Mom,Grandm ...
谁是initiating loader -
chong_zh:
例子中的Cindy,Mom,Grandma并没有载入委派关系, ...
谁是initiating loader -
beyondqinghua:
有时间慢慢看,收藏先!
几种排序算法的对比
引用
Programming: String Mutation
Richard Dawkins, in his book The Blind Watchmaker, explains his much-debated experiment known as the weasel program. This algorithm is designed to show that random mutations in DNA can create complex life forms through natural selection, rather than the will of an intelligent designer. His experiment took a sample string of gibberish and repeatedly mutated, or slightly changed, the string as if introducing an error in DNA duplication from one generation to another. The program had a "target" string in mind -- a line from Hamlet, "Methinks it is like a weasel" -- and continued until the random mutations added up such that the original string was the same as this target string. By running his program and finding that any line of gibberish can become Shakespeare through incremental changes, Dawkins suggested that natural selection can account for all the complexity of life.
Naturally, this is a controversial experiment, and an over-simplification of evolution. One criticism is that it is designed to run toward a target string; at every generation, a string's "fitness" to reproduce is determined by its similarity to the target. Could the target not be determined by an intelligent designer? And is an organism's fitness to survive proportional in such small increments to its genetic similarity with a better-adapted mutant? Whatever the answer, Dawkins probably did not anticipate that his experiment was an early example of what has become a common technique in machine learning: genetic algorithms. GAs can apply the same strategy effectively to complicated problems in artifical intelligence: come up with a model, make a lot of "daughter" models that are slightly different, choose the daughter that works a little bit better than the original model, and make her the new mother for the next generation. Lather, rinse, repeat.
For HW2 programming we're going to try our hand at genetic algorithms, using the same string-to-string transformation goal. Specifically, the steps of your program should be:
Take from the user, on the command line, two strings: an input string to start from, and a target string toward which to mutate. (Hint: when testing on CUNIX, you can use quotation marks to pass command line arguments with spaces. See example output.)
Model the input word as a linked list of letters (Strings of length 1), using Java's Linked List class.
Create a large set of mutated "daughter" strings from the input string, each of which is different in a small way:
Insertion. For each of 27 letters (including space), and each of N+1 positions in the input string, insert the letter into the word at the position. Use the add() method of LinkedList that takes an index to quickly insert the letter at the correct position. For a string of N letters, this method will produce 27 * (N+1) daughter strings. For example HELLO becomes AHELLO, BHELLO, etc., until HELLOZ (not counting the space character).
Deletion. For each letter in the input string, delete the letter. Use the remove() method of LinkedList() to do this quickly. For a string of N letters, this method will produce N daughter strings. For example, HELLO becomes ELLO, HLLO, HELO (twice) and HELL.
Choose the daughter string that is closest to the target string. To do this, use an algorithm to find each daughter string's fitness -- a score between 0 and 1 -- and find the daughter string with the highest score.
Fitness should be calculated as the length of the longest common substring of the target string found in the input string, divided by the length of the target string. For example, HPPLE has a score of .8 compared to APPLE, since the longest substring of APPLE found in HPPLE is 4 letters long, which is .8 of APPLE's length. Additionally, penalize the score by 0.1 for each extraneous letter the input string has over the length of the target string. For example. HAPPLE has a score of .9 compared to APPLE, because it is 1 letter longer, and otherwise has the complete substring APPLE. Note that this makes it possible for a candidate word to have a negative score.
Print out the generation number, the winning daughter string and her score.
Repeat the above from step 2, making the winning daughter the new input string, until the daughter has reached the target string. You can use either iteration or recursion to repeat appropriately. Then, terminate the program.
Here are some hints and guidelines for your program:
Let's be a little more formal about object-oriented design in HW2. Set up a class called Word which will represent a single word. The class should:
Translate an input string into a LinkedList in the constructor. It should take a String as an argument in the constructor, have a private LinkedList as a variable, and parse the string into the LinkedList (using the substring() method of String).
Have a method, mutate, which returns an array of all daughter Words to be sired by the target string. That is, each daughter should be a new Word.
Have a method makeCopy() that duplicates itself into an identical Word. Your mutate() function will use this to create many copies of itself and then change each copy.
Have a method toString() that creates a normal string from the LinkedList and returns it. (Hint: use a StringBuffer.)
Have one other class, MutationSim, that takes the input and target strings from the user, creates a Word from the target, mutates it, calculates fitness, and repeats as described above.
Learn about the String methods substring(), toUpperCase() and indexOf().
It'll help if you create a function that returns all the letter of the alphabet in an array over which you can iterate in a loop... something maybe like this:
Answer the following questions in your README:
How did you design your program? What design choices did you make in setting up your algorithm, other than the ones specified above?
How long does your program take? What do you think is the maximum number of generations needed to reach the target string, i.e., Big-Oh for an input string of I letters, a target string of T letters and an alphabet of L letters? Is this also the minimum generations needed? Why or why not?
How does the total run time relate to you answer to #2? How do you see your program's performance change as you increase the length of the strings?
Do you think the fitness function was a fair model of natural selection? Why or why not? How would you improve it?
Example Output
Richard Dawkins, in his book The Blind Watchmaker, explains his much-debated experiment known as the weasel program. This algorithm is designed to show that random mutations in DNA can create complex life forms through natural selection, rather than the will of an intelligent designer. His experiment took a sample string of gibberish and repeatedly mutated, or slightly changed, the string as if introducing an error in DNA duplication from one generation to another. The program had a "target" string in mind -- a line from Hamlet, "Methinks it is like a weasel" -- and continued until the random mutations added up such that the original string was the same as this target string. By running his program and finding that any line of gibberish can become Shakespeare through incremental changes, Dawkins suggested that natural selection can account for all the complexity of life.
Naturally, this is a controversial experiment, and an over-simplification of evolution. One criticism is that it is designed to run toward a target string; at every generation, a string's "fitness" to reproduce is determined by its similarity to the target. Could the target not be determined by an intelligent designer? And is an organism's fitness to survive proportional in such small increments to its genetic similarity with a better-adapted mutant? Whatever the answer, Dawkins probably did not anticipate that his experiment was an early example of what has become a common technique in machine learning: genetic algorithms. GAs can apply the same strategy effectively to complicated problems in artifical intelligence: come up with a model, make a lot of "daughter" models that are slightly different, choose the daughter that works a little bit better than the original model, and make her the new mother for the next generation. Lather, rinse, repeat.
For HW2 programming we're going to try our hand at genetic algorithms, using the same string-to-string transformation goal. Specifically, the steps of your program should be:
Take from the user, on the command line, two strings: an input string to start from, and a target string toward which to mutate. (Hint: when testing on CUNIX, you can use quotation marks to pass command line arguments with spaces. See example output.)
Model the input word as a linked list of letters (Strings of length 1), using Java's Linked List class.
Create a large set of mutated "daughter" strings from the input string, each of which is different in a small way:
Insertion. For each of 27 letters (including space), and each of N+1 positions in the input string, insert the letter into the word at the position. Use the add() method of LinkedList that takes an index to quickly insert the letter at the correct position. For a string of N letters, this method will produce 27 * (N+1) daughter strings. For example HELLO becomes AHELLO, BHELLO, etc., until HELLOZ (not counting the space character).
Deletion. For each letter in the input string, delete the letter. Use the remove() method of LinkedList() to do this quickly. For a string of N letters, this method will produce N daughter strings. For example, HELLO becomes ELLO, HLLO, HELO (twice) and HELL.
Choose the daughter string that is closest to the target string. To do this, use an algorithm to find each daughter string's fitness -- a score between 0 and 1 -- and find the daughter string with the highest score.
Fitness should be calculated as the length of the longest common substring of the target string found in the input string, divided by the length of the target string. For example, HPPLE has a score of .8 compared to APPLE, since the longest substring of APPLE found in HPPLE is 4 letters long, which is .8 of APPLE's length. Additionally, penalize the score by 0.1 for each extraneous letter the input string has over the length of the target string. For example. HAPPLE has a score of .9 compared to APPLE, because it is 1 letter longer, and otherwise has the complete substring APPLE. Note that this makes it possible for a candidate word to have a negative score.
Print out the generation number, the winning daughter string and her score.
Repeat the above from step 2, making the winning daughter the new input string, until the daughter has reached the target string. You can use either iteration or recursion to repeat appropriately. Then, terminate the program.
Here are some hints and guidelines for your program:
Let's be a little more formal about object-oriented design in HW2. Set up a class called Word which will represent a single word. The class should:
Translate an input string into a LinkedList in the constructor. It should take a String as an argument in the constructor, have a private LinkedList as a variable, and parse the string into the LinkedList (using the substring() method of String).
Have a method, mutate, which returns an array of all daughter Words to be sired by the target string. That is, each daughter should be a new Word.
Have a method makeCopy() that duplicates itself into an identical Word. Your mutate() function will use this to create many copies of itself and then change each copy.
Have a method toString() that creates a normal string from the LinkedList and returns it. (Hint: use a StringBuffer.)
Have one other class, MutationSim, that takes the input and target strings from the user, creates a Word from the target, mutates it, calculates fitness, and repeats as described above.
Learn about the String methods substring(), toUpperCase() and indexOf().
It'll help if you create a function that returns all the letter of the alphabet in an array over which you can iterate in a loop... something maybe like this:
public static String[] alphabet () { String[] alphabet = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", " "}; return alphabet; }
Answer the following questions in your README:
How did you design your program? What design choices did you make in setting up your algorithm, other than the ones specified above?
How long does your program take? What do you think is the maximum number of generations needed to reach the target string, i.e., Big-Oh for an input string of I letters, a target string of T letters and an alphabet of L letters? Is this also the minimum generations needed? Why or why not?
How does the total run time relate to you answer to #2? How do you see your program's performance change as you increase the length of the strings?
Do you think the fitness function was a fair model of natural selection? Why or why not? How would you improve it?
Example Output
$ java MutationSim "Hello there" "How are you" Generation 1: ELLO THERE (score: 0.18181818181818182) Generation 2: ELLOW THERE (score: 0.2727272727272727) Generation 3: LLOW THERE (score: 0.2727272727272727) Generation 4: LLHOW THERE (score: 0.36363636363636365) Generation 5: LHOW THERE (score: 0.36363636363636365) Generation 6: LHOW ATHERE (score: 0.45454545454545453) Generation 7: HOW ATHERE (score: 0.45454545454545453) Generation 8: HOW ARTHERE (score: 0.5454545454545454) Generation 9: HOW ARHERE (score: 0.5454545454545454) Generation 10: HOW ARERE (score: 0.6363636363636364) Generation 11: HOW ARE RE (score: 0.7272727272727273) Generation 12: HOW ARE YRE (score: 0.8181818181818182) Generation 13: HOW ARE YE (score: 0.8181818181818182) Generation 14: HOW ARE YOE (score: 0.9090909090909091) Generation 15: HOW ARE YO (score: 0.9090909090909091) Generation 16: HOW ARE YOU (score: 1.0)
$ java MutationSim "it was a dark and stormy night" "to be or not to be" Generation 1: IT WAS A DARK AND TORMY NIGHT (score: -0.9333333333333335) Generation 2: T WAS A DARK AND TORMY NIGHT (score: -0.8333333333333334) Generation 3: WAS A DARK AND TORMY NIGHT (score: -0.7333333333333334) Generation 4: WAS A DARK AND TORMY NIGHT (score: -0.6333333333333334) Generation 5: AS A DARK AND TORMY NIGHT (score: -0.5333333333333334) Generation 6: S A DARK AND TORMY NIGHT (score: -0.43333333333333346) Generation 7: A DARK AND TORMY NIGHT (score: -0.33333333333333337) Generation 8: A DARK AND TORMY NIGHT (score: -0.23333333333333336) Generation 9: DARK AND TORMY NIGHT (score: -0.1333333333333334) Generation 10: DARK AND TORMY NIGHT (score: -0.033333333333333354) Generation 11: ARK AND TORMY NIGHT (score: 0.06666666666666665) Generation 12: RK AND TORMY NIGHT (score: 0.16666666666666666) Generation 13: K AND TORMY NIGHT (score: 0.16666666666666666) Generation 14: K ANDT TORMY NIGHT (score: 0.2222222222222222) Generation 15: ANDT TORMY NIGHT (score: 0.2222222222222222) Generation 16: ANDOT TORMY NIGHT (score: 0.2777777777777778) Generation 17: ANOT TORMY NIGHT (score: 0.3333333333333333) Generation 18: NOT TORMY NIGHT (score: 0.3888888888888889) Generation 19: R NOT TORMY NIGHT (score: 0.4444444444444444) Generation 20: OR NOT TORMY NIGHT (score: 0.5) Generation 21: OR NOT TOMY NIGHT (score: 0.5) Generation 22: OR NOT TOMY NIGHT (score: 0.5555555555555556) Generation 23: OR NOT TOY NIGHT (score: 0.5555555555555556) Generation 24: OR NOT TO NIGHT (score: 0.6111111111111112) Generation 25: E OR NOT TO NIGHT (score: 0.6666666666666666) Generation 26: BE OR NOT TO NIGHT (score: 0.7222222222222222) Generation 27: BE OR NOT TO IGHT (score: 0.7222222222222222) Generation 28: BE OR NOT TO IGHT (score: 0.7777777777777778) Generation 29: BE OR NOT TO GHT (score: 0.7777777777777778) Generation 30: O BE OR NOT TO GHT (score: 0.8333333333333334) Generation 31: O BE OR NOT TO HT (score: 0.8333333333333334) Generation 32: TO BE OR NOT TO HT (score: 0.8888888888888888) Generation 33: TO BE OR NOT TO T (score: 0.8888888888888888) Generation 34: TO BE OR NOT TO BT (score: 0.9444444444444444) Generation 35: TO BE OR NOT TO B (score: 0.9444444444444444) Generation 36: TO BE OR NOT TO BE (score: 1.0)
$ java MutationSim asafsdfkurweafasfp "methinks it is like a weasel" Generation 1: ASAFSDFKUR WEAFASFP (score: 0.14285714285714285) Generation 2: ASAFSDFKURA WEAFASFP (score: 0.17857142857142858) Generation 3: ASAFSDFKUR A WEAFASFP (score: 0.21428571428571427) Generation 4: ASAFSDFKURE A WEAFASFP (score: 0.25) Generation 5: ASAFSDFKURKE A WEAFASFP (score: 0.2857142857142857) Generation 6: ASAFSDFKURIKE A WEAFASFP (score: 0.32142857142857145) Generation 7: ASAFSDFKURLIKE A WEAFASFP (score: 0.35714285714285715) Generation 8: ASAFSDFKUR LIKE A WEAFASFP (score: 0.39285714285714285) Generation 9: ASAFSDFKURS LIKE A WEAFASFP (score: 0.42857142857142855) Generation 10: ASAFSDFKURIS LIKE A WEAFASFP (score: 0.4642857142857143) Generation 11: SAFSDFKURIS LIKE A WEAFASFP (score: 0.4642857142857143) Generation 12: SAFSDFKUR IS LIKE A WEAFASFP (score: 0.5) Generation 13: AFSDFKUR IS LIKE A WEAFASFP (score: 0.5) Generation 14: AFSDFKURT IS LIKE A WEAFASFP (score: 0.5357142857142857) Generation 15: FSDFKURT IS LIKE A WEAFASFP (score: 0.5357142857142857) Generation 16: FSDFKURIT IS LIKE A WEAFASFP (score: 0.5714285714285714) Generation 17: SDFKURIT IS LIKE A WEAFASFP (score: 0.5714285714285714) Generation 18: SDFKUR IT IS LIKE A WEAFASFP (score: 0.6071428571428571) Generation 19: DFKUR IT IS LIKE A WEAFASFP (score: 0.6071428571428571) Generation 20: DFKURS IT IS LIKE A WEAFASFP (score: 0.6428571428571429) Generation 21: FKURS IT IS LIKE A WEAFASFP (score: 0.6428571428571429) Generation 22: FKURKS IT IS LIKE A WEAFASFP (score: 0.6785714285714286) Generation 23: KURKS IT IS LIKE A WEAFASFP (score: 0.6785714285714286) Generation 24: KURNKS IT IS LIKE A WEAFASFP (score: 0.7142857142857143) Generation 25: URNKS IT IS LIKE A WEAFASFP (score: 0.7142857142857143) Generation 26: URINKS IT IS LIKE A WEAFASFP (score: 0.75) Generation 27: RINKS IT IS LIKE A WEAFASFP (score: 0.75) Generation 28: RHINKS IT IS LIKE A WEAFASFP (score: 0.7857142857142857) Generation 29: HINKS IT IS LIKE A WEAFASFP (score: 0.7857142857142857) Generation 30: THINKS IT IS LIKE A WEAFASFP (score: 0.8214285714285714) Generation 31: THINKS IT IS LIKE A WEAASFP (score: 0.8214285714285714) Generation 32: THINKS IT IS LIKE A WEASFP (score: 0.8571428571428571) Generation 33: ETHINKS IT IS LIKE A WEASFP (score: 0.8928571428571429) Generation 34: METHINKS IT IS LIKE A WEASFP (score: 0.9285714285714286) Generation 35: METHINKS IT IS LIKE A WEASP (score: 0.9285714285714286) Generation 36: METHINKS IT IS LIKE A WEASEP (score: 0.9642857142857143) Generation 37: METHINKS IT IS LIKE A WEASE (score: 0.9642857142857143) Generation 38: METHINKS IT IS LIKE A WEASEL (score: 1.0)
MutationSim.java:
/* * MutationSim.java, 02/22/2008 */ public class MutationSim { private String input; private String target; private static String USAGE = "Usage: java MutationSim INPUT_STRING TARGET_STRING" + "\nOnly English alphabet and space is allowed in the strings."; public static void main(String[] args) { MutationSim sim = new MutationSim(); try { sim.processCommandLine(args); sim.runSim(); } catch (Exception e) { System.err.println(e.getMessage()); e.printStackTrace(System.err); usage(); } } private void processCommandLine(String[] args) throws Exception { if (args.length > 2) throw new Exception("Too many arguments."); if (args.length < 2) throw new Exception("Too little arguments."); if (isNonAlphabetExist(args[0]) || isNonAlphabetExist(args[1])) throw new Exception("Non-English alphabet character in string."); this.input = args[0]; this.target = args[1]; } void runSim() { Word word = new Word(this.input); Word targetWord = new Word(this.target); Word currentWord = null; double score = -2.0; // dummy value for (int generation = 1; score != 1.0; ++generation) { Word[] daughters = word.mutate(); // iterate through the array, // do a linear search for best fit double maxFitness = -2.0; for (Word w : daughters) { double currentFitness = calculateFitness(w, targetWord); if (maxFitness <= currentFitness) { currentWord = w; maxFitness = currentFitness; } } printStat(generation, currentWord, maxFitness); word = currentWord; score = maxFitness; } } static void usage() { System.err.println(); System.err.println(USAGE); } static void printStat(int generation, Word word, double score) { System.out.println("Generation " + generation + ": " + word.toString() + " {score: " + score + ")"); } public static double calculateFitness(Word in, Word targ) { double ratio = Util.longestCommonSubstring(in.toString(), targ.toString()).length() / (double) targ.length(); double penalty = (in.length() > targ.length()) ? (0.1 * (in.length() - targ.length())) : 0; return ratio - penalty; } private static boolean isNonAlphabetExist(String s) { s = s.toUpperCase(); String[] alphabet = Util.alphabet(); for (String letter : alphabet) { s = s.replaceAll(letter, ""); } return (s.length() != 0); } }
Word.java:
/* * Word.java, 02/22/2008 */ import java.util.ArrayList; import java.util.LinkedList; /** * */ public class Word { private LinkedList<String> list; public Word() { // list = null; } public Word(String input) { list = new LinkedList<String>(); for (int i = 0; i < input.length(); ++i) { String currentLetter = input.substring(i, i+1); list.add(currentLetter.toUpperCase()); } } public Word[] mutate() { String[] alphabet = Util.alphabet(); int wordLength = this.list.size(); int alphabetLength = alphabet.length; int arraySize = (wordLength + 1) * alphabetLength + wordLength; Word[] result = new Word[arraySize]; int index = 0; for (int i = 0; i <= wordLength; ++i) { // insertion for (int j = 0; j < alphabetLength; ++j) { Word copy = this.makeCopy(); copy.list.add(i, alphabet[j]); result[index++] = copy; } // deletion if (i < this.list.size()) { Word copy = this.makeCopy(); copy.list.remove(i); result[index++] = copy; } } return result; } public Word makeCopy() { Word copy = new Word(); copy.list = (LinkedList<String>) this.list.clone(); return copy; } public int length() { return this.list.size(); } @Override public String toString() { StringBuilder builder = new StringBuilder(); for (String str : list) { builder.append(str); } return builder.toString(); } }
Util.java:
public class Util { /** * Returns the longest common substring of the given two arguments. * * @param first the first string * @param second the second string * @return the longest common substring of the given two arguments */ public static String longestCommonSubstring(String first, String second) { // could have used dynamic programming, or generalized suffix tree // to solve the LCS problem, but here we'll just stick to simplicity int start = 0; // The start in a of the longest found so far int len = 0; // The length of the longest found so far for (int i = 0; i < first.length() - len; ++i) { for (int j = first.length(); j > i + len; --j) { if (second.indexOf(first.substring(i, j)) != -1) { start = i; len = j - i; break; // Exit the inner loop } } } return first.substring(start, start + len); } public static String[] alphabet() { String[] alphabet = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", " " }; return alphabet; } }
相关推荐
1.字符串操作演示(Visual C++编程 源代码)1.字符串操作演示(Visual C++编程 源代码)1.字符串操作演示(Visual C++编程 源代码)1.字符串操作演示(Visual C++编程 源代码)1.字符串操作演示(Visual C++编程 源...
C#字符串删除指定字符串|C#字符串删除子字符串
java 字符串转16进制 16进制转字符串 将两个ASCII字符合成一个字节; java 字符串转16进制 16进制转字符串 将两个ASCII字符合成一个字节; java 字符串转16进制 16进制转字符串 将两个ASCII字符合成一个字节; java ...
* 使用一个字符串分割另一个字符串 * * @param delimiter 边界上的分隔字符 * @param haystack 输入的字符串 * @param out 输出的字符串指针 * @return 分割成了多少个成员 */ int explode(char *delimiter, ...
Delphi 7.0 提取字符串中指定子字符串后的字符串,这个平时在字符处理时候使用几率也挺高的,获取指定字符串后面的字符串,比如获取扩展名等也可以用此方法,本例中要用到After函数,测试时,当单击按钮时,执行以下...
编写程序:从键盘上输入一个包含10个字符的字符串,把该字符串与程序中给定的字符串("bacdbcabca") //依次比较,统计两个字符串对应字符相等的数目。然后输出从键盘上输入的字符串, //并把两个字符串中对应字符不...
51接收字符串演示程序.rar 单片机可以接受超长字符串,当然字符串来自PC,其他的也可以, 收到后再原封不动的发回来,本代码12MHz,波特率为4800,有11.0592M的话可以更完美,(9600下可以误差0%,当然的你自己去改本...
给写了2个方法,一个是直接截取单个需要的字符串,比如字符串string a="ab123456",我只需要提取3,那么就是单独截取就可以了,从2开始到4结束就行。 第二个是把所有的符合条件的字符串都截取出来,提取出来,比如...
字符串操作完全演示
indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置。如果要检索的字符串值没有出现,则该方法返回 -1。 方法二:match() var str = "123" var reg = RegExp(/3/); if(str.match(reg)){ //包含; } ...
纯C语言实现字符串拆分操作,把字符串拆分成字符串数组,然后再输出。 使用sstream方法,作为底层驱动操作实现非常方便。
本文实例讲述了javascript实现的字符串与十六进制表示字符串相互转换方法。分享给大家供大家参考。具体如下: 之所以写这个,是因为发现SQL注入和XSS中经常利用十六进制表示的字符串,比如 SELECT CONCAT(0x68656c6...
一个关于字符串匹配的算法,已经经过编译,希望对你有帮助
求两个字符串的最长公共字符串 输出全部位置信息,并输出字符串,相同字符串先输出所有位置信息在输出字符串 测试平台:XP/VS 2008 CN
这个代码可以添加一个新的字符串到已有的字符串数组中,并确保不会重复添加相同的字符串。具体来说,它首先创建了一个包含3个字符串的字符串数组`strArray`,然后定义了一个新的字符串`newStr`。接着,使用`ismember...
必须实现如下操作,字符串比较、求串的长度、判断串是否为空、将串置空、字符串赋值(包括两个字符串类复制,一个字符串赋值到CmyString对象)、求字符串中的一个字符或改变字符串中的一个字符(采用重载[]),...
串口接收字符串控制LED。通过STM32 串口1 接收字符串实现LED控制,程序通过判断\n识别字符串是否发送完成,所以串口调试助手上需勾选发送新行,发送LED1+ON点亮LED1,发送LED1+OFF关闭LED1
string常用截取字符串方法有很多,但是配合使用以下两种,基本都能满足要求: find(string strSub, npos); find_last_of(string strSub, npos); 其中strSub是需要寻找的子字符串,npos为查找起始位置。找到返回子...
易语言字符串操作源码,字符串操作,字符串_取长度,字符串_取中间,字符串_取左边,字符串_取右边,字符串_替换,到宽字符,到多字节,取文本数据地址,取字节集数据地址,MultiByteToWideChar,WideCharToMultiByte
输入一个字符串参数,返回该字符串的反序字符串