8 kyu

altERnaTIng cAsE <=> ALTerNAtiNG CaSe

18,773 of 67,215donaldsebleung

Description:

altERnaTIng cAsE <=> ALTerNAtiNG CaSe

Define String.prototype.toAlternatingCase (or a similar function/method such as to_alternating_case/toAlternatingCase/ToAlternatingCase in your selected language; see the initial solution for details) such that each lowercase letter becomes uppercase and each uppercase letter becomes lowercase. For example:

"hello world".toAlternatingCase() === "HELLO WORLD"
"HELLO WORLD".toAlternatingCase() === "hello world"
"hello WORLD".toAlternatingCase() === "HELLO world"
"HeLLo WoRLD".toAlternatingCase() === "hEllO wOrld"
"12345".toAlternatingCase()       === "12345"                   // Non-alphabetical characters are unaffected
"1a2b3c4d5e".toAlternatingCase()  === "1A2B3C4D5E"
"String.prototype.toAlternatingCase".toAlternatingCase() === "sTRING.PROTOTYPE.TOaLTERNATINGcASE"
toAlternatingCase("hello world") === "HELLO WORLD"
toAlternatingCase("HELLO WORLD") === "hello world"
toAlternatingCase("hello WORLD") === "HELLO world"
toAlternatingCase("HeLLo WoRLD") === "hEllO wOrld"
toAlternatingCase("12345")       === "12345" // Non-alphabetical characters are unaffected
toAlternatingCase("1a2b3c4d5e")  === "1A2B3C4D5E"
"hello world".to_alternating_case() === "HELLO WORLD"
"HELLO WORLD".to_alternating_case() === "hello world"
"hello WORLD".to_alternating_case() === "HELLO world"
"HeLLo WoRLD".to_alternating_case() === "hEllO wOrld"
"12345".to_alternating_case()       === "12345"                 # Non-alphabetical characters are unaffected
"1a2b3c4d5e".to_alternating_case()  === "1A2B3C4D5E"
"String.prototype.toAlternatingCase".to_alternating_case() === "sTRING.PROTOTYPE.TOaLTERNATINGcASE"
"hello world".to_alternating_case() === "HELLO WORLD"
"HELLO WORLD".to_alternating_case() === "hello world"
"hello WORLD".to_alternating_case() === "HELLO world"
"HeLLo WoRLD".to_alternating_case() === "hEllO wOrld"
"12345".to_alternating_case()       === "12345"                # Non-alphabetical characters are unaffected
"1a2b3c4d5e".to_alternating_case()  === "1A2B3C4D5E"
"String.prototype.toAlternatingCase".to_alternating_case() === "sTRING.PROTOTYPE.TOaLTERNATINGcASE"
StringUtils.toAlternativeString("hello world") == "HELLO WORLD"
StringUtils.toAlternativeString("HELLO WORLD") == "hello world"
StringUtils.toAlternativeString("hello WORLD") == "HELLO world"
StringUtils.toAlternativeString("HeLLo WoRLD") == "hEllO wOrld"
StringUtils.toAlternativeString("12345") == "12345" // Non-alphabetical characters are unaffected
StringUtils.toAlternativeString("1a2b3c4d5e") == "1A2B3C4D5E"
StringUtils.toAlternativeString("StringUtils.toAlternatingCase") == "sTRINGuTILS.TOaLTERNATINGcASE"
toAlternatingCase "hello world" `shouldBe` "HELLO WORLD"
toAlternatingCase "HELLO WORLD" `shouldBe` "hello world"
toAlternatingCase "hello WORLD" `shouldBe` "HELLO world"
toAlternatingCase "HeLLo WoRLD" `shouldBe` "hEllO wOrld"
toAlternatingCase "12345"       `shouldBe` "12345"
toAlternatingCase "1a2b3c4d5e"  `shouldBe` "1A2B3C4D5E"
"hello world".ToAlternatingCase() == "HELLO WORLD"
"HELLO WORLD".ToAlternatingCase() == "hello world"
"hello WORLD".ToAlternatingCase() == "HELLO world"
"HeLLo WoRLD".ToAlternatingCase() == "hEllO wOrld"
"12345".ToAlternatingCase() == "12345" // Non-alphabetical characters are unaffected
"1a2b3c4d5e".ToAlternatingCase() == "1A2B3C4D5E"
"String.ToAlternatingCase".ToAlternatingCase() == "sTRING.tOaLTERNATINGcASE"
alter_case("hello world") == "HELLO WORLD"
alter_case("HELLO WORLD") == "hello world"
alter_case("hello WORLD") == "HELLO world"
alter_case("HeLLo WoRLD") == "hEllO wOrld"
alter_case("12345")       == "12345"                        # Non-alphabetical characters are unaffected
alter_case("1a2b3c4d5e")  == "1A2B3C4D5E"
alter_case("StringUtils.toAlternatingCase") == "sTRINGuTILS.TOaLTERNATINGcASE"
to_alternating_case("hello world") # => "HELLO WORLD"
to_alternating_case("HELLO WORLD") # => "hello world"
to_alternating_case("hello WORLD") # => "HELLO world"
to_alternating_case("HeLLo WoRLD") # => "hEllO wOrld"
to_alternating_case("12345") # => "12345" (Non-alphabetical characters are unaffected)
to_alternating_case("1a2b3c4d5e") # => "1A2B3C4D5E"
to_alternating_case("String.prototype.toAlternatingCase") # => "sTRING.PROTOTYPE.TOaLTERNATINGcASE"
to_alternating_case("hello world"); // => "HELLO WORLD"
to_alternating_case("HELLO WORLD"); // => "hello world"
to_alternating_case("hello WORLD"); // => "HELLO world"
to_alternating_case("HeLLo WoRLD"); // => "hEllO wOrld"
to_alternating_case("12345"); // => "12345" (Non-alphabetical characters are unaffected)
to_alternating_case("1a2b3c4d5e"); // => "1A2B3C4D5E"
to_alternating_case("String.prototype.toAlternatingCase"); // => "sTRING.PROTOTYPE.TOaLTERNATINGcASE"
string source = "HeLLo WoRLD";
string upperCase = to_alternating_case(source);
cout << upperCase << endl; // outputs: hEllO wOrld
toAlternatingCase("hello world") === "HELLO WORLD"
toAlternatingCase("HELLO WORLD") === "hello world"
toAlternatingCase("hello WORLD") === "HELLO world"
toAlternatingCase("HeLLo WoRLD") === "hEllO wOrld"
toAlternatingCase("12345")       === "12345"                   // Non-alphabetical characters are unaffected
toAlternatingCase("1a2b3c4d5e")  === "1A2B3C4D5E"
toAlternatingCase("String.prototype.toAlternatingCase") === "sTRING.PROTOTYPE.TOaLTERNATINGcASE"
"hello world" >alternating ! "HELLO WORLD"
"HELLO WORLD" >alternating ! "hello world"
"hello WORLD" >alternating ! "HELLO world"
"HeLLo WoRLD" >alternating ! "hEllO wOrld"
"12345" >alternating ! "12345" (Non-alphabetical characters are unaffected)
"1a2b3c4d5e" >alternating ! "1A2B3C4D5E"
"String.prototype.toAlternatingCase" >alternating ! "sTRING.PROTOTYPE.TOaLTERNATINGcASE"
ToAlternatingCase("hello world"); // => "HELLO WORLD"
ToAlternatingCase("HELLO WORLD"); // => "hello world"
ToAlternatingCase("hello WORLD"); // => "HELLO world"
ToAlternatingCase("HeLLo WoRLD"); // => "hEllO wOrld"
ToAlternativeCase("12345"); // => "12345" (Non-alphabetical characters are unaffected)
ToAlternativeCase("1a2b3c4d5e"); // => "1A2B3C4D5E"
ToAlternativeCase("String.prototype.toAlternatingCase"); // => "sTRING.PROTOTYPE.TOaLTERNATINGcASE"
to_alternating_case "hello world" --> "HELLO WORLD"
to_alternating_case "HELLO WORLD" --> "hello world"
to_alternating_case "hello WORLD" --> "HELLO world"
to_alternating_case "HeLLo WoRLD" --> "hEllO wOrld"
to_alternating_case "12345" --> "12345" (Non-alphabetical characters are unaffected)
to_alternating_case "1a2b3c4d5e" --> "1A2B3C4D5E"
to_alternating_case "String.prototype.toAlternatingCase" --> "sTRING.PROTOTYPE.TOaLTERNATINGcASE"

As usual, your function/method should be pure, i.e. it should not mutate the original string.

Fundamentals

Stats:

CreatedMar 21, 2016
PublishedMar 21, 2016
Warriors Trained108200
Total Skips10449
Total Code Submissions160333
Total Times Completed67215
JavaScript Completions18773
Ruby Completions1790
Python Completions23486
Haskell Completions732
C# Completions3337
Java Completions6985
Elixir Completions368
CoffeeScript Completions29
C Completions1589
Rust Completions1428
C++ Completions7194
Crystal Completions23
NASM Completions25
TypeScript Completions1183
Factor Completions29
Go Completions2079
Julia Completions37
Dart Completions121
Lua Completions37
COBOL Completions6
Total Stars803
% of votes with a positive feedback rating90% of 5319
Total "Very Satisfied" Votes4427
Total "Somewhat Satisfied" Votes743
Total "Not Satisfied" Votes149
Ad
Contributors
  • donaldsebleung Avatar
  • smepple Avatar
  • bkaes Avatar
  • cris Avatar
  • GiacomoSorbi Avatar
  • raulbc777 Avatar
  • anter69 Avatar
  • LazyWolf Avatar
  • alessandrodalbello Avatar
  • kazk Avatar
  • B1ts Avatar
  • lilsweetcaligula Avatar
  • a.merezhanyi Avatar
  • Voile Avatar
  • Souzooka Avatar
  • user8654865 Avatar
  • kroppt Avatar
  • FArekkusu Avatar
  • monadius Avatar
  • marh Avatar
  • hobovsky Avatar
  • solitude Avatar
  • user8436785 Avatar
  • ejini战神 Avatar
  • user2058606 Avatar
  • akar-0 Avatar
  • Sigmanificient Avatar
  • metatable Avatar
Ad