Count Words in CamelCase String

Duration: 4 min

This video lesson is available to enrolled students.

Enroll to watch — LTI MINDTREE Superset Course

AI summary & chapters

AI Summary

An AI-generated summary of this video lecture.

This video is a programming tutorial from Knowledge Gate that teaches how to count words in a string based on specific capitalization rules. The instructor first presents a problem where the first word is entirely lowercase, and all subsequent words start with an uppercase letter followed by lowercase letters. The task is to count the total number of words in the string. The solution involves iterating through the string and identifying word boundaries by checking for uppercase letters. The instructor then demonstrates the solution in C, using a for loop and an if condition to check if a character is an uppercase letter (ASCII 65-90). The code is implemented and tested in an online C compiler, where the input "saveChangesInTheEditor" correctly outputs 5 words, and another test case "iAmShubam" outputs 3 words. The video concludes with a discussion on the logic and a final code run.

Chapters

  1. 0:00 2:00 00:00-02:00

    The video begins with the Knowledge Gate logo, followed by a slide detailing a programming problem. The problem states that a sequence of words is written with the first word in lowercase and all subsequent words starting with an uppercase letter and the rest in lowercase. The task is to count the number of words in a given string. The input is a single line of text, and the output is the word count. The constraints specify the string length is between 1 and 10^5. An example is provided: the input "saveChangesInTheEditor" should output 5, and "iAmShubam" should output 3. The instructor, Yash Jain Sir, explains the problem, emphasizing that the first word is all lowercase and each subsequent word starts with an uppercase letter. He uses a red pen to circle the first word and the uppercase letters in the subsequent words, visually reinforcing the rule. He then explains that the solution involves counting the number of uppercase letters, which indicates the start of a new word, and adding one for the first word.

  2. 2:00 3:38 02:00-03:38

    The instructor transitions to coding the solution in C. The code is displayed on a screen, showing the inclusion of string.h and stdio.h, and the main function. A character array `st[10000]` is declared to hold the input string, and an integer `cnt` is initialized to 0. The `scanf` function reads the input string. A for loop iterates through the string using `strlen(st)`. Inside the loop, an if condition checks if the current character `st[i]` is an uppercase letter by verifying if its ASCII value is between 65 and 90. If true, the counter `cnt` is incremented. After the loop, `printf("%d", cnt+1);` is used to print the total word count, which is the number of uppercase letters plus one for the first word. The instructor explains this logic, highlighting the `cnt` variable and the `printf` statement. The code is then run in an online C compiler. The input "saveChangesInTheEditor" is entered, and the output is 5. The instructor then tests another input, "iAmShubam", which correctly outputs 3, confirming the solution works.

The video provides a clear, step-by-step tutorial on solving a word counting problem with specific capitalization rules. It starts with a well-defined problem statement, uses visual aids to explain the pattern, and then implements a logical solution in C. The core concept is that the number of words is equal to the number of uppercase letters (indicating new words) plus one for the initial lowercase word. The demonstration in the online compiler validates the solution, making it a practical and effective learning resource.

Loading lesson…