String programs in C

  • String programs in C are a group of characters, numbers, and symbols that are enclosed in double quotes called a string.
  • Ex: “India”
  • A string is also called an array of characters or character array.
  • Every string is terminated with ‘\0’ or NULL characters. 
  • Each character of the string occupies 1 byte of memory.
  • The last character of a string is \0. The compiler automatically puts \0 at the end of the string even though we have not specified it.
string programs in c

String Syntax in C:

Data type array name [size];

 Ex: char str[10];

Initializing the string in C:

Ex:  char str[] = { ‘r’,’a’,’m’,’a’,’\0’};

       char str[5] = {‘r’,’a’,’m’,’a’};

       char str[] = “rama”;

       char str[5] = “rama”;

Reading the String programs in C:

  • In order to read the string from the keyboard we can use a formatted or unformatted function.
  • The formatted function is

               scanf(“%s”, &str);

               The unformatted function is

               Gets(str);

Displaying the String programs in C:
  • The string programs in C can be displayed on the screen either by using formatted functions or unformatted functions.
  • The formatted function is

               printf(“%s”, str);

               The unformatted function is

               Puts(str);

Displaying the string programs in C in different formats:

The string can be displayed in different formats by using only the formatted function.

Char str[15] = “prabhakar”

Printf(“%s”, str); prabhakar

Printf(“%4s”, str); prabhakar

Printf(“%10s”, str); prabhakar

Printf(“%.4s”, str); prab

Printf(“%.6s”, str); prabha

Printf(“%10.4s”, str); _ _ _ _ _ _ prab

Printf(“%-10.4s”, str); prab

palindrome string program in c

#include <stdio.h>
#include <string.h>  // For strlen() function
int main() {
    char str[100];  // Array to store the input string
    int i, len, flag = 0;  // Variables for indexing, length, and palindrome check
    // Get input from the user
    printf("Enter a string: ");
    scanf("%s", str); 
    // Calculate the length of the string (excluding the null terminator)
    len = strlen(str);  
    // Check if the string is a palindrome
    for (i = 0; i < len / 2; i++) { 
        // Compare characters from the beginning and end, moving inwards
        if (str[i] != str[len - i - 1]) { 
            // If any pair of characters doesn't match, it's not a palindrome
            flag = 1;  // Set the flag to indicate non-palindrome
            break;     // Exit the loop early
        }
    }
    // Display the result based on the flag value
    if (flag == 0) {
        printf("%s is a palindrome.\n", str);
    } else {
        printf("%s is not a palindrome.\n", str);
    }
    return 0; // Indicate successful program execution
}

Output:


Enter a string: garduate
garduate is not a palindrome.

Example program to accept and display the string using formatted functions

#include <stdio.h>
#include <string.h>
int main() {
    char name[50]; // Character array to store the string (max 49 characters)
    // Input using fgets() to prevent buffer overflow
    printf("Enter your name: ");
    fgets(name, sizeof(name), stdin); // Read input string, including spaces
    // Remove trailing newline character from fgets()
    name[strcspn(name, "\n")] = 0; 
    // Display using printf()
    printf("Hello, %s!\n", name); 
    // Display the length of the string using the strlen() function
    printf("Your name has %zu characters.\n", strlen(name));
    return 0;
}

Output

Enter your name:

Example program to accept and display the string using unformatted functions.

#include <stdio.h>
int main() {
    char user_input[100]; // Assuming maximum input length is 100 characters
    // Accepting input from the user
    printf("Enter a string: ");
    fgets(user_input, sizeof(user_input), stdin);
    // Displaying the string without formatted functions
    printf("You entered: ");
    fputs(user_input, stdout); // Using fputs to print the string
    return 0;
}

Output

Enter a string: 

Scroll to Top