0% found this document useful (0 votes)
6 views2 pages

String Manipulation 2

String manipulation involves operations on strings in C, such as copying, concatenating, and comparing. Common functions include strlen, strcpy, strcat, strcmp, and strrev. The document also provides a code example to check if a string is a palindrome, demonstrating input and output for both a palindrome and a non-palindrome string.

Uploaded by

royrian149
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

String Manipulation 2

String manipulation involves operations on strings in C, such as copying, concatenating, and comparing. Common functions include strlen, strcpy, strcat, strcmp, and strrev. The document also provides a code example to check if a string is a palindrome, demonstrating input and output for both a palindrome and a non-palindrome string.

Uploaded by

royrian149
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

String Manipulation

String manipulation refers to the process of performing operations on strings, such as copying,
concatenating, comparing, finding length, reversing, or replacing characters.

In C, strings are represented as arrays of characters terminated by a null character (\0).

Common String Functions in C

1. strlen(str): Finds the length of the string.

2. strcpy(dest, src): Copies one string into another.

3. strcat(dest, src): Concatenates two strings.

4. strcmp(str1, str2): Compares two strings.

5. strrev(str): Reverses a string (available in some compilers like Turbo C).

---

Program: Check if a String is Palindrome

A palindrome is a string that reads the same forward and backward (e.g., "madam", "level").

Code Example

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

int main() {
char str[100], reversed[100];
int length, isPalindrome = 1;
printf("Enter a string: ");
scanf("%s", str);
length = strlen(str);
for (int i = 0; i < length; i++)
{
reversed[i] = str[length - i - 1];
}
reversed[length] = '\0';
for (int i = 0; i < length; i++)
{
if (str[i] != reversed[i])
{
isPalindrome = 0;
}
}
if (isPalindrome)
{

printf("The string '%s' is a palindrome.\n", str);


}
else
{

printf("The string '%s' is not a palindrome.\n", str);


}

return 0;
}

Sample Input and Output

Input:

Enter a string: madam

Output:

The string 'madam' is a palindrome.

Input:

Enter a string: hello

Output:

The string 'hello' is not a palindrome.

You might also like