Home > Software engineering >  How do I write a function to count the characters in a string?
How do I write a function to count the characters in a string?

Time:02-04

I'm working a practice exercise for a class, and I've reached an impasse. The instructions state:

Write a function that takes a string of text and counts the number of characters. The function should return "There are xx characters in that string."

This is what I have thus far:

w <- "I hope everyone has a good weekend"


answer <- function (nchar) { 
  statement <- paste("There are", nchar, "characters in that string")
}

I've tried plugging "w" into the function to see if it works, but I'm getting no results. Please bear in mind that I'm new to R.

But I've been wracking my brain over this. Can someone give me a clue as to what I'm missing? Many thanks for any help provided.

CodePudding user response:

nchar is your function to count the number of characters in a string. If you don't want to count the whitespace you could use gsub to remove them from your string and count again the characters. You could use the following code:

w <- "I hope everyone has a good weekend"

answer <- function (x) { 
  statement <- paste("There are", nchar(x), "characters in that string")
  statement
}
answer(w)
#> [1] "There are 34 characters in that string"

answer2 <- function (x) { 
  statement <- paste("There are", nchar(gsub(" ", "",x))
, "characters in that string")
  statement
}
answer2(w)
#> [1] "There are 28 characters in that string"

Created on 2023-02-03 with reprex v2.0.2

CodePudding user response:

You are confusing the function

nchar()

with your function input

Look at the following:

w <- "I hope everyone has a good weekend"

answer <- function (myInputString) { statement <- paste("There are", nchar(myInputString), "characters in that string")
return(statement) }

Note that you also missed to add return at the end of your function, to specify what the output should be.

Good luck with you journey into coding ;)

CodePudding user response:

Just for a bit of fun - and for you to try to work out what is going on - here are some alternative functions that give the same answer as the built-in nchar but don't actually use it...

This one splits it into a list of single characters, converts it to a vector, and returns the length...

nchar1 <- function(s) length(unlist(str_split(s, "")))

This one converts it into RAW format (a vector of the byte values that are used to encode the string) and returns the length...

nchar2 <- function(s) length(charToRaw(s))

This one uses a while loop to see at which point the substring function substr returns an empty string...

nchar3 <- function(s){
  i <- 0
  while(substr(s, i 1, i 2) != ""){
    i <- i 1
    }
  return(i)
}

This one uses a similar approach to count how many times we can remove the first character before getting to an empty string...

nchar4 <- function(s){
  i <- 0
  while(s != ""){
    s <- sub(".", "", s)
    i <- i   1
  }
  return(i)
}

This one might make your head hurt a bit. It uses a similar technique to the last one but uses Recall to call itself until it gets to the point (a blank string) at which it returns an answer.

nchar5 <- function(s, n = 0){
  if(s == "") {
    return(n)
  } else {
    Recall(sub(".", "", s), n   1)
  }
}

nchar1("Good luck!")
[1] 10
nchar2("Good luck!")
[1] 10
nchar3("Good luck!")
[1] 10
nchar4("Good luck!")
[1] 10
nchar5("Good luck!")
[1] 10
  • Related