Skip to the content.

Big Idea 3 Unit 4

# Python examples
# Popcorn Hack Python
# Finding number of characters in last name using Len.
yay = len("Muralikrishnan")
print(yay) # Output is 14

# Using concat to merge first and last name
name = "Nikith" + "Muralikrishnan"
print(name) # Output is Nikith Muralikrishnan

# Using substring to show 3rd to 6th letters of my name
substring = name[2:6]
print(substring) # Output is kith


# Homework Python
def character_count(input_string): 
    total_letters = 0
    total_numbers = 0
    total_spaces = 0  # sets all variables to 0

    for char in input_string:
        if char.isalpha():
            total_letters += 1  # If the character in the input string is a letter, the letter count increases by 1
        elif char.isdigit():
            total_numbers += 1  # If character in string is a number, the number variable increases by 1
        elif char.isspace():
            total_spaces += 1   # if the character in the string is a space, the space variable increases by 1

    return total_letters, total_numbers, total_spaces  #returns the final number of letters, numbers, and spaces

# Get user input
user_input = input("Enter string: ")   # gets input
letters, numbers, spaces = character_count(user_input)

# Print the results
print(f"Total letters: {letters}") # Prints number of letters
print(f"Total numbers: {numbers}")  # Prints number of numbers
print(f"Total spaces: {spaces}") # prints number of spaces
// Javascript Examples
// JS Popcorn Hack
let last_name = "Muralikrishnan"; // setting last_name equal to Muralikrishnan
let the_length = last_name.length; 

console.log(the_length); // Output would be 15

let a_name = "Nikith " + "Muralikrishnan";
console.log(a_name); // Output would be Nikith Muralikrishnan

let a_substring = a_name.substring(1, 8);
console.log(a_substring); // Output would be ikith M

// JS homework
function characterCount(inputString) { 
    let totalLetters = 0;
    let totalNumbers = 0;
    let totalSpaces = 0; // sets all variables to 0

    for (let char of inputString) {
        if (char.match(/[a-zA-Z]/)) {
            totalLetters += 1;  // If the character is a letter, increase the letter count
        } else if (char.match(/[0-9]/)) {
            totalNumbers += 1;  // If character is a number, increase the number count
        } else if (char === ' ') {
            totalSpaces += 1;   // If the character is a space, increase the space count
        }
    }

    return [totalLetters, totalNumbers, totalSpaces]; // returns the counts in an array
}

// Get user input
const userInput = prompt("Enter a string:"); // gets input from user
const [letters, numbers, spaces] = characterCount(userInput); // Call the function

// Print the results
console.log(`Total letters: ${letters}`);   // Prints number of letters
console.log(`Total numbers: ${numbers}`);   // Prints number of numbers
console.log(`Total spaces: ${spaces}`);     // Prints number of spaces