In R, Switch Uppercase to Lowercase and Vice-Versa in a String

In R, switch uppercase to lowercase and vice-versa in a string

I'm sort of curious if there is a better way than:

chartr(x = this,
old = paste0(c(letters,LETTERS),collapse = ""),
new = paste0(c(LETTERS,letters),collapse = ""))

Helpful observation by @Joris in the comments that ?chartr notes that you can use character ranges, avoiding the paste:

chartr("a-zA-Z", "A-Za-z",this)

How to make a function which change lowercase to uppercase and vice verse?

Example string

mystring = "This Is aN ExAmPlE sTRING"

See ?chartr for details but its used like this chartr(old,new,x). Here we are flipping the lower case and uppercase letters so the old is the reverse of the new and what we want to change (x) is the string.
Function:

myfunct <- function(string){
chartr("a-zA-Z", "A-Za-z",string)
}

call function:

myfunct(mystring)

Output:

[1] "tHIS iS An eXaMpLe String"

Changing lowercase characters to uppercase and vice-versa in Python

def swap_case(s):
swapped = []

for char in s:
if char.islower():
swapped.append(char.upper())
elif char.isupper():
swapped.append(char.lower())
else:
swapped.append(char)

return ''.join(swapped)

How to convert characters of a string from lowercase to upper case and vice versa without using string functions in python?

The string method .join() can help you to unite a list and return a string. But without that knowledge, you could have done this string concatenation.(For that you need to initialize new_string with "", not [])

Join usage:

"".join(["h","e","l","l","o"])
# "hello"

To your second question. You could check if an input is from the alphabet with the .isalpha() method. Which returns a boolean value.

"a".isalpha()
# True

"&".isalpha()
# False

And a suggestion about the solution, You could import the uppercase and lowercase alphabets from the string module. After that, iterating over the term and swapping letters using the alphabet strings is very easy. Your solution is fine for understanding how ascii table works. But with the way I mentioned, you can avoid facing problems about special cases. It is a poor method for cryptology though.

Convert upper chars to lower and lower to upper (vice versa)

Here's a regular expression solution, which takes advantage of the fact that upper and lowercase letters differ by the bit corresponding to decimal 32:

var testString = 'heLLo World 123',    output;
output= testString.replace(/([a-zA-Z])/g, function(a) { return String.fromCharCode(a.charCodeAt() ^ 32); }) document.body.innerHTML= output;

Swap Cases- switch from lowercase to uppercase and viceversa

There are multiple issues with this code,

  1. The array should not run to length, so it should be < instead of <=

  2. There is no need for 3 loops

  3. result.join should join with an empty string instead of blank space

  4. Intendations are all over the place.

see corrected code below:

function SwapCase(str) { 
let cap=str.toUpperCase()
let low=str.toLowerCase()
let arr=str.split("")
let result=[]

for(var i=0; i<arr.length;i++){
if(arr[i]===cap[i]){
result.push(arr[i].toLowerCase())
}
if(arr[i]===low[i]){
result.push(arr[i].toUpperCase())
}
}
return result.join("")
}

console.log(SwapCase("heLLo"))


Related Topics



Leave a reply



Submit