私はいくつかのテストケースでこの関数を書きました:
characterCounter <- function(char1, char2) {
if(is.null(char1) || is.null(char2)) {
print("Please check your character sequences!")
return()
}
if(nchar(char1, type = "chars") < nchar(char2, type = "chars") || nchar(char1, type = "chars") <= nchar(char2, type = "chars")) {
cat(sprintf("%s is smaller or smaller-equal than %s\n", char1 , char2))
return()
} else if(nchar(char1, type = "chars") > nchar(char2, type = "chars") || nchar(char1, type = "chars") >= nchar(char2, type = "chars")) {
cat(sprintf("%s is greater or greater-equal than %s\n", char1 , char2))
return()
} else if(nchar(char1, type = "chars") == nchar(char2, type = "chars")) {
cat(sprintf("%s is equal to %s\n", char1, char2))
return()
}
}
#Testcases
(characterCounter("Hello","Hell"))
(characterCounter("Wor","World"))
ただし、各ケースの後、私は戻ってきます:
> (characterCounter("Hello","Hell"))
Hello is greater or greater-equal than Hell
NULL
> (characterCounter("Wor","World"))
Wor is smaller or smaller-equal than World
NULL
私の出力で気に入らないのは、末尾のNULL
. なぜ私はこれを取り戻すのですか?(文字カウンター(NULL,NULL))
アップデート
characterCounter <- function(char1, char2) {
if(is.null(char1) || is.null(char2)) {
return(cat("Please check your character sequences!"))
}
if(nchar(char1, type = "chars") < nchar(char2, type = "chars") || nchar(char1, type = "chars") <= nchar(char2, type = "chars")) {
return(cat(sprintf("%s is smaller or smaller-equal than %s\n", char1 , char2)))
} else if(nchar(char1, type = "chars") > nchar(char2, type = "chars") || nchar(char1, type = "chars") >= nchar(char2, type = "chars")) {
return(cat(sprintf("%s is greater or greater-equal than %s\n", char1 , char2)))
} else if(nchar(char1, type = "chars") == nchar(char2, type = "chars")) {
return(cat(sprintf("%s is equal to %s\n", char1, char2)))
}
}