🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Introduction
In JavaScript, counting the number of characters in a string is a simple task. This is often useful when working with form inputs, text processing, or validating data. JavaScript provides the length property, which can be used to determine the number of characters in a string, including spaces and special characters.
Problem Statement
Create a JavaScript program that:
- Accepts a string.
- Counts the number of characters in the string, including spaces and special characters.
- Returns and displays the character count.
Example:
Input:
"Hello World"Output:
11Input:
"JavaScript is awesome!"Output:
22
Solution Steps
- Read the Input String: Provide the string either via user input or directly in the code.
- Count the Characters: Use the
lengthproperty of the string to count the number of characters. - Display the Result: Print the character count.
JavaScript Program
// JavaScript Program to Count the Number of Characters in a String
// Author: https://www.javaguides.net/
function countCharacters(str) {
// Step 1: Return the length of the string
return str.length;
}
// Example input
let inputString = "Hello World";
let characterCount = countCharacters(inputString);
console.log(`The number of characters in the string is: ${characterCount}`);
Output
The number of characters in the string is: 11
Example with Different Input
let inputString = "JavaScript is awesome!";
let characterCount = countCharacters(inputString);
console.log(`The number of characters in the string is: ${characterCount}`);
Output:
The number of characters in the string is: 22
Explanation
Step 1: Use the length Property
- The
lengthproperty of a string in JavaScript returns the total number of characters in the string, including spaces and special characters.
Step 2: Return and Display the Result
- The character count is returned by the function and displayed using
console.log().
Example with Extra Whitespaces
If the string contains extra spaces, they will be counted as characters as well:
let inputString = " Hello World ";
let characterCount = countCharacters(inputString);
console.log(`The number of characters in the string is: ${characterCount}`);
Output:
The number of characters in the string is: 17
Conclusion
This JavaScript program demonstrates how to count the number of characters in a string using the length property. This method is efficient and includes all characters, such as spaces, punctuation, and special characters. It's a simple yet powerful approach to counting characters in strings for various applications such as text validation and processing.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment