capitalize Nth character
in a string
[ slice | toLowerCase | toUpperCase ]

Capitalize Nth character in a string

Write a function that takes a string and number and returns a new string with the Nth character of the string capitalized.

The rest of the characters in the word are converted to lower case.

If the Nth character is not an alphabetic character the character is returned as is.


Example ...

Enter a string ...

"Spill the Beans" original string

2 position of character to capitalize

"sPill the beans" returns new string with 2nd character capitalized

The Nth character of the string is capitalized.

"P" character capitalized

All other characters in the string are converted to lower case.

The original string is unchanged.

Strings are a sequence of zero or more characters written inside quotes used to represent text.

Strings may consist of letters, numbers, symbols, words, or sentences.

Strings are immutable, they cannot be changed.

Each character in a string has an index.

The first character will be index 0 the second character will be index 1 and so on.

There are two ways to access an individual character in a string.

charAt() method

const str1 = "abc"; string

str1.charAt(0); character at index 0 → "a"

str1.charAt(1); character at index 1 → "b"

str1.charAt(2); character at index 2 → "c"

str1.charAt(3); character at index 3 → "" index not found

Alternatively use at() or slice() methods

bracket notation []

const str2 = "abc"; string

str2[0]; character at index 0 → "a"

str2[1]; character at index 1 → "b"

str2[2]; character at index 2 → "c"

str2[3]; character at index 3 → undefined index not found


Numbers are used to represent both integer and floating-point values.

Numbers are most commonly expressed in literal forms like 255 or 3.14159 ↴

let num1 = 5; → number

let num2 = 2.5; → number

let num3 = num1 + num2;

console.log(num3); returns ↴

7.5 → number


Capitalize Nth character of a string using ↴

slice() method → extracts a part of a string and returns it as a new string, without modifying the original string.

toLowerCase() method → returns the value of the string converted to lower case.

toUpperCase() method → returns the value of the string converted to upper case.


slice() method extracts a part of a string and returns it as a new string, without modifying the original string.

syntax ↴

slice(start) return a new string from start index to end of string

slice(start, end) return a string from start index to end index of string (exclusive).

Return a new string from index 1 to end of string.

const str1 = "Hello World";

str1.slice(1); start index is 1

returns ↴

"ello World"

Return a new string from index 1 to index 9 (exclusive).

const str2 = "Hello World";

str2.slice(1, 9); start index is 1 end index is 9 (not included)

returns ↴

"ello Wor"


toLowerCase() method converts all letters to lower case. The original string is unchanged.

const str3 = "hELlo wORLd";

str3.toLowerCase(); returns ↴

"hello world" → lower case


toUpperCase() method converts all letters to upper case. The original string is unchanged.

const str4 = "hELlo wORLd";

str4.toUpperCase(); returns ↴

"HELLO WORLD" → upper case


Initialize a variable to hold the string to capitalize the Nth character.

const string1 = "Hello World"; → user input

Initialize a variable to hold the Nth position.

const num1 = 5; → user input


Define a function capitalizeNthChar to capitalize the Nth character of a string.

function capitalizeNthChar(str, num) {}

The function takes a string and number as input str, num and returns a new string with the Nth character of string capitalized. The original string remains unchanged.

Check if the provided number is within the valid range, between 1 and string length.

if (num > 0 && num <= str.length) {}

Convert the string to lower case.

let lowerStr = str.toLowerCase() lowerStr

Strings are zero-indexed, meaning the first character is at index 0, the second at index 1, and so on.

num is the Nth character to find.

Index of position 1 will be num - 1 1 - 1 [0]

Index of position 2 will be num - 1 2 - 1 [1]

Index of position 3 will be num - 1 3 - 1 [2]

and so on ...

Get the Nth character and convert it to upper case.

const nthChar = lowerStr[num - 1].toUpperCase() nthChar

Return the modified string with the Nth character capitalized.

return lowerStr.slice(0, num - 1) + nthChar + lowerStr.slice(num)

slice(0, num - 1) string from index 0 to Nth character (exclusive).

nthChar Nth character to capitalize.

slice(num) string from num to end of string.


Call the function with ↴

capitalizeNthChar(string1, num1);


Capitalize 5th character in string.

const string1 = "hello world";

const num1 = 5;

function capitalizeNthChar(str, num) {

if (num > 0 && num <= str.length) {

let lowerStr = str.toLowerCase();

const nthChar = lowerStr[num - 1].toUpperCase();

return lowerStr.slice(0, num - 1) + nthChar + lowerStr.slice(num);

}

}

call function

capitalizeNthChar(string1, num1); returns ↴

"hellO world" 5th character capitalized

Capitalize Nth character

position of Nth character