2081 3

BIT102 ยท 2081 ยท 10 marks

What is string?Explain any three string function in "string.h" header file in C with example.Write a program to check whether a word is palindrome or not.[2+3+5]

What is string?Explain any three string function in "string.h" header file in C with example.Write a program to check whether a word is palindrome or not.[2+3+5]

C ProgrammingChapter 6: Arrays and StringsString definition and declaration

Quick answer

A string in C is a sequence of characters stored in a character array and terminated by a null character (\0). C does not have a built-in string data type; instead, strings are represented as arrays of type char.

String, String Functions, and Palindrome Check


1. What is String? [2 marks]

Declaration:

char name[] = "Hello";
// Stored as: H e l l o \0

The null character \0 marks the end of the string and is automatically added when a string literal is used.


2. Three String Functions in string.h [3 marks]

i) strlen() - String Length

Returns the number of characters in a string, excluding the null character.

#include <stdio.h>
#include <string.h>
int main() {
    char str[] = "Hello";
    printf("Length = %d", strlen(str));  // Output: 5
    return 0;
}

ii) strcpy() - String Copy

Copies the content of one string (source) into another string (destination).

#include <stdio.h>
#include <string.h>
int main() {
    char src[] = "Nepal";
    char dest[20];
    strcpy(dest, src);
    printf("Copied string: %s", dest);  // Output: Nepal
    return 0;
}

iii) strcmp() - String Compare

Compares two strings lexicographically.

  • Returns 0 if both strings are equal
  • Returns a negative value if first string is less than second
  • Returns a positive value if first string is greater than second
#include <stdio.h>
#include <string.h>
int main() {
    char s1[] = "apple";
    char s2[] = "apple";
    if (strcmp(s1, s2) == 0)
        printf("Strings are equal");   // Output: Strings are equal
    else
        printf("Strings are not equal");
    return 0;
}

3. Program to Check Whether a Word is Palindrome or Not [5 marks]

Logic: A word is a palindrome if it reads the same forwards and backwards. Example: "madam", "level", "racecar"

We compare the string with its reverse. If they are equal, it is a palindrome.

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

int main() {
    char word[100], rev[100];
    int len, i, j;
    int isPalindrome = 1;  // Assume true initially

    printf("Enter a word: ");
    scanf("%s", word);

    len = strlen(word);

    // Reverse the word manually
    for (i = 0, j = len - 1; i < len; i++, j--) {
        rev[i] = word[j];
    }
    rev[len] = '\0';  // Add null terminator

    // Compare original and reversed word
    if (strcmp(word, rev) == 0) {
        printf("\"%s\" is a Palindrome.\n", word);
    } else {
        printf("\"%s\" is NOT a Palindrome.\n", word);
    }

    return 0;
}

Sample Output:

Case 1:

Enter a word: madam
"madam" is a Palindrome.

Case 2:

Enter a word: hello
"hello" is NOT a Palindrome.

Explanation of Key Steps:

StepDescription
strlen(word)Finds the length of the entered word
Reverse loopCopies characters from end to start into rev[]
rev[len] = '\0'Properly terminates the reversed string
strcmp(word, rev)Compares original and reversed; returns 0 if equal

Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.