I'm trying to use R to find the start difference of two strings, i.e. from which letter these two strings become different, and hope the function can give me the location number. The function always give the value 2, and seems the loop only runs one time.
Here is my code:
string1 = "CGCGGTGCATCCTGGGAGTTGTAGTTTTTTCTACTCAGAGGGAGAATAGCTCCAGACGGGAGCAGGATGA"
string2 = "CGCGGTGCATCCTGGGATGTAGTTTTTTCTACTCAGAGGGAGAATAGCTCCAGACGGGAGCAGGATGA"
location <- function(string1, string2){
len1 = nchar(string1)
len2 = nchar(string2)
len = max(len1, len2)
score = 1
i = 1
if (i <= len){
if (substring(string1, i, i) == substring(string2, i, i)){
score = score + 1
i = i + 1
}
else if (substring(string1, i, i) != substring(string2, i, i)){
break
}
}
return(score)
}
location(string1, string2)
Thank you very much!