Hebrew words affecting order in cat statement

Here is a little code that counts letters in English words:
words <- c("miscognizable", "harvestry", "geopolitist", "jessed", "pardonee", "whitfield", "ghazal", "morphophonemically", "calonectria","conceptiveness")
for (i in 1:length(words)) {
char_count <- nchar(words[i])
cat(words[i], ":", char_count, "characters\n", sep = " ")
}

miscognizable : 13 characters
harvestry : 9 characters
geopolitist : 11 characters
jessed : 6 characters
pardonee : 8 characters
whitfield : 9 characters
ghazal : 6 characters
morphophonemically : 18 characters
calonectria : 11 characters
conceptiveness : 14 characters

To my surprise the code works with Hebrew words, which are written right to left, except that the cat print statement is printing word, colon, and count right to left. How I can I correct this?

words <- c("מלך", "ממשלה", "פרח", "מחשב", "תפוח", "כתב", "בית", "שקט", "צהריים", "שמיים")
for (i in 1:length(words)) {
char_count <- nchar(words[i])
cat(words[i], ":", char_count, "characters\n", sep = " ")
}

3 characters
ממשלה : 5 characters
פרח : 3 characters
מחשב : 4 characters
תפוח : 4 characters
כתב : 3 characters
בית : 3 characters
שקט : 3 characters
צהריים : 6 characters
שמיים : 5 characters

Locale dependent?

words <- c("מלך", "ממשלה", "פרח", "מחשב", "תפוח", "כתב", "בית", "שקט", "צהריים", "שמיים")
for (i in 1:length(words)) {
  char_count <- nchar(words[i])
  cat(words[i], ":", char_count, "characters\n", sep = " ")
}
#> מלך : 3 characters
#> ממשלה : 5 characters
#> פרח : 3 characters
#> מחשב : 4 characters
#> תפוח : 4 characters
#> כתב : 3 characters
#> בית : 3 characters
#> שקט : 3 characters
#> צהריים : 6 characters
#> שמיים : 5 characters

# note LOCALE
sessionInfo()
#> R version 4.2.2 (2022-10-31)
#> Platform: aarch64-apple-darwin20 (64-bit)
#> Running under: macOS Ventura 13.2.1
#> 
#> Matrix products: default
#> BLAS:   /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/lib/libRblas.0.dylib
#> LAPACK: /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/lib/libRlapack.dylib
#> 
#> locale:
#> [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> loaded via a namespace (and not attached):
#>  [1] digest_0.6.31     withr_2.5.0       R.methodsS3_1.8.2 lifecycle_1.0.3  
#>  [5] magrittr_2.0.3    reprex_2.0.2      evaluate_0.20     rlang_1.1.0      
#>  [9] cli_3.6.1         rstudioapi_0.14   fs_1.6.1          R.utils_2.12.2   
#> [13] R.oo_1.25.0       vctrs_0.6.1       styler_1.9.1      rmarkdown_2.21   
#> [17] tools_4.2.2       R.cache_0.16.0    glue_1.6.2        purrr_1.0.1      
#> [21] xfun_0.38         yaml_2.3.7        fastmap_1.1.1     compiler_4.2.2   
#> [25] htmltools_0.5.5   knitr_1.42

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

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.