C Program to Find Total Words in a String

Introduction

This article will provide you with a C program to find total words in a string.

In the world of programming, the ability to manipulate strings is a fundamental skill. One common task is to find the total number of words in a given string.

Also Read: C Program To Read Two Files Simultaneously

So let’s dive in and explore the intricacies of the “C program to find total words in a string.”

What is a String?

Before we delve into the details of the program, let’s briefly discuss what a string is in the context of programming.

Also Read: Armstrong Number in C Programming

In C, a string is a sequence of characters enclosed within double quotes, such as “Hello, World!” or “OpenAI is amazing!” Strings are widely used to store and manipulate textual data in programs.

The Program: Finding Total Words in a String

To begin with, let’s take a look at the C program that calculates the total number of words in a given string:

#include <stdio.h>

int countWords(char str[]) {
    int count = 1; // Assuming the string has at least one word
    int i;

    for (i = 0; str[i] != '\0'; i++) {
        if (str[i] == ' ' || str[i] == '\n' || str[i] == '\t') {
            count++;
        }
    }

    return count;
}

int main() {
    char str[100];

    printf("Enter a string: ");
    fgets(str, sizeof(str), stdin);

    printf("Total words in the string: %d\n", countWords(str));

    return 0;
}

Let’s break down the program and understand how it works.

1. Including the Necessary Libraries

The program begins with the inclusion of the <stdio.h> library, which provides the standard input-output functions required for the program to work.

Also Read: C Program to Find the Inverse of 2×2 Matrix

2. Defining the countWords Function

The countWords function takes a string as input and returns the total number of words in that string.

It accomplishes this by iterating through each character in the string and counting the number of spaces, newline characters, and tabs it encounters.

Also Read: C Program to Copy the Contents of One File into Another File

The assumption here is that words in the string are separated by either a space, a newline, or a tab.

3. Implementing the main Function

The main function is where the actual execution of the program takes place. It prompts the user to enter a string and stores it in the str variable using the fgets function.

The fgets function reads characters from the standard input (keyboard) and stores them in the specified string.

Also Read: Getchar and Putchar Function in C with Example

Once the string is obtained, the program calls the countWords function and passes the string as an argument.

The return value of countWords is then displayed as the output, representing the total number of words in the given string.

Also Read: Best 5 Programs on Fibonacci Series in C

FAQs

1. How does the program count the words in a string?

The program counts the words in a string by iterating through each character and checking for spaces, newline characters, and tabs. Whenever it encounters any of these characters, it increments the word count.

2. Can the program handle multiple spaces between words?

Yes, the program can handle multiple spaces between words. It counts words based on the presence of spaces, newline characters, and tabs. So, even if there are multiple spaces between words, the program will still count them as individual words.

3. What happens if the string is empty?

If the string entered by the user is empty, i.e., it does not contain any characters, the program will still assume it has one word. This is because an empty string can be considered as a single word.

4. How can I modify the program to count words differently?

If you wish to modify the program to count words differently, you can customize the condition inside the if statement in the countWords function. By changing the conditions, you can count words based on different criteria, such as punctuation marks or specific characters.

5. Are uppercase and lowercase characters treated differently?

No, the program treats uppercase and lowercase characters in the same way. It counts words based on spaces, newline characters, and tabs, regardless of the case of the characters.

6. Can this program be used in other programming languages?

The program provided here is written in the C programming language. However, the logic behind counting words in a string can be applied to other programming languages as well. You may need to modify the syntax and function calls according to the language you are using.

Also Read: Program To Reverse a String in C using Pointer

Conclusion

In this article, we explored a C program that efficiently counts the total number of words in a given string.

By understanding the program’s logic and implementation, you can enhance your programming skills and be better equipped to handle string manipulation tasks.

Also Read: Find the Runner Up Score | Hackerrank Solution

Remember to experiment with the program and modify it according to your specific requirements.

So go ahead, try out the “C program to find total words in a string,” and unlock the power of string manipulation in your programming journey.

Also Read: 25 Tricky Questions on Pointers in C: Explained and Answered