C Programs

C Programming – Arrays


Programs at a Glance

This section lists the programs covered under String Handling in C. Click on a program title to jump directly to its detailed explanation.

  1. Demonstration of String Handling Functions
  2. Palindrome Check for a String
  3. Concatenation of Three Strings
  4. Count Lines, Words and Characters
  5. Position of a Character in a String

Demonstration of String Handling Functions


Problem Statement

Write a C program to demonstrate basic string handling functions.

Algorithm
  1. Start the program.
  2. Declare two character arrays to store strings.
  3. Read a string from the user.
  4. Find and display the length of the string using a library function.
  5. Copy the original string into another string variable.
  6. Reverse the original string in place using a library function.
  7. Display the copied and reversed strings.
  8. Stop the program.
C Program

#include <stdio.h>
#include <string.h>

int main() {
    char s1[50], s2[50];

    printf("Enter a string:\n");
    gets(s1);   // Note: gets is unsafe; used here only for simple demonstration

    printf("Length of string = %lu\n", strlen(s1));

    strcpy(s2, s1);
    printf("Copied string = %s\n", s2);

    strrev(s1);
    printf("Reversed string = %s\n", s1);

    return 0;
}
    
Sample Output

Enter a string:
hello
Length of string = 5
Copied string = hello
Reversed string = olleh
    
Explanation

The function strlen() counts how many characters are present in the string before the null terminator '\0', giving the length of the string. The strcpy() function copies all characters from the source string to the destination string, including the terminating null character.

The function strrev() reverses the characters of the string in place, so the first character becomes the last and vice versa. The same character array is reused to show how the content of a string can be modified by library functions.

Palindrome Check for a String


Problem Statement

Write a C program to check whether a given string is a palindrome or not.

Algorithm
  1. Start the program.
  2. Declare two character arrays to store the original and reversed strings.
  3. Read the original string from the user.
  4. Copy the original string into another array.
  5. Reverse the copied string using a string function.
  6. Compare the original and reversed strings using a comparison function.
  7. If both are equal, display that the string is a palindrome.
  8. Otherwise, display that it is not a palindrome.
  9. Stop the program.
C Program

#include <stdio.h>
#include <string.h>

int main() {
    char s[50], rev[50];

    printf("Enter a string:\n");
    gets(s);   // Note: gets is unsafe; used here only for simple demonstration

    strcpy(rev, s);
    strrev(rev);

    if (strcmp(s, rev) == 0)
        printf("Palindrome String\n");
    else
        printf("Not a Palindrome String\n");

    return 0;
}
    
Sample Output

Enter a string:
madam
Palindrome String
    
Explanation

A palindrome string reads the same from left to right and from right to left. The original string is copied into another array and then reversed, so the two strings can be compared without losing the original.

The function strcmp() returns 0 when both strings have exactly the same sequence of characters. If this condition is satisfied, the program prints that the string is a palindrome; otherwise, it indicates that it is not.

Concatenation of Three Strings


Problem Statement

Write a C program to concatenate three strings.

Algorithm
  1. Start the program.
  2. Declare three character arrays to store the input strings.
  3. Read the first string from the user.
  4. Read the second string from the user.
  5. Read the third string from the user.
  6. Append the second string to the end of the first string.
  7. Append the third string to the end of the updated first string.
  8. Display the final concatenated string.
  9. Stop the program.
C Program

#include <stdio.h>
#include <string.h>

int main() {
    char s1[100], s2[50], s3[50];

    printf("Enter first string:\n");
    gets(s1);   // Note: gets is unsafe; used here only for simple demonstration

    printf("Enter second string:\n");
    gets(s2);

    printf("Enter third string:\n");
    gets(s3);

    strcat(s1, s2);
    strcat(s1, s3);

    printf("Concatenated string = %s\n", s1);

    return 0;
}
    
Sample Output

Enter first string:
Hello
Enter second string:
World
Enter third string:
2025
Concatenated string = HelloWorld2025
    
Explanation

The function strcat() appends the characters of the second string to the end of the first string, starting at the null terminator. After the first concatenation, s1 holds the combined content of the first and second strings.

A second call to strcat() appends the third string to the updated s1, producing one continuous string. It is important that the destination array s1 is large enough to hold all characters from the three input strings plus the final null terminator.

Count Lines, Words and Characters


Problem Statement

Write a C program to count the number of lines, words and characters in a given text.

Algorithm
  1. Start the program.
  2. Initialize counters for lines, words and characters to zero.
  3. Read the input text one character at a time until end-of-file (EOF).
  4. For every character read, increment the character counter.
  5. Whenever a space or newline is read, increment the word counter.
  6. Whenever a newline is read, increment the line counter.
  7. After EOF is reached, display the number of lines, words and characters.
  8. Stop the program.
C Program

#include <stdio.h>

int main() {
    char ch;
    int lines = 0, words = 0, chars = 0;

    printf("Enter text (end with EOF):\n");

    while ((ch = getchar()) != EOF) {
        chars++;
        if (ch == ' ' || ch == '\n')
            words++;
        if (ch == '\n')
            lines++;
    }

    printf("Lines = %d\nWords = %d\nCharacters = %d\n", lines, words, chars);

    return 0;
}
    
Sample Output

Enter text (end with EOF):
Welcome to C programming
This is a test
^Z
Lines = 2
Words = 6
Characters = 39
    
Explanation

The getchar() function reads the input stream one character at a time until EOF is encountered. Each character contributes to the character count, while separators such as spaces and newlines are used to approximate word boundaries.

Every newline increments both the line counter and, through the condition for spaces/newlines, the word counter as well. This simple logic is sufficient for basic text statistics, although more advanced word counting might handle multiple spaces and punctuation more carefully.

Position of a Character in a String


Problem Statement

Write a C program that displays the position of a character ch in a string S or −1 if the string does not contain ch.

Algorithm
  1. Start the program.
  2. Declare a character array for the string and a character variable for ch.
  3. Read the string from the user.
  4. Read the character to be searched.
  5. Initialize a position variable to −1 to indicate “not found”.
  6. Scan the string from left to right, comparing each character with ch.
  7. If a match is found, set position to the current index (plus 1 for human-readable position) and stop searching.
  8. Display the position value (or −1 if no match was found).
  9. Stop the program.
C Program

#include <stdio.h>

int main() {
    char s[50], ch;
    int i, pos = -1;

    printf("Enter a string:\n");
    gets(s);   // Note: gets is unsafe; used here only for simple demonstration

    printf("Enter a character:\n");
    scanf("%c", &ch);

    for (i = 0; s[i] != '\0'; i++) {
        if (s[i] == ch) {
            pos = i + 1;  // Positions counted from 1
            break;
        }
    }

    printf("Position = %d\n", pos);
    return 0;
}
    
Sample Output

Enter a string:
education
Enter a character:
a
Position = 5
    
Explanation

The program performs a linear search over the string, comparing each character with the target character ch. As soon as a match is found, the loop terminates and the position (1-based index) is reported.

If no character in the string matches ch, the position remains −1, indicating that the string does not contain the specified character. This approach clearly distinguishes between “found” and “not found” cases for the user.