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

C String Operations: Length, Reverse, Palindrome

The document provides a C program that performs various string operations including calculating the length of a string, reversing a string, checking the equality of two strings, verifying if a string is a palindrome, and checking for a substring. Each operation is demonstrated with code snippets and example outputs. The program showcases fundamental string manipulation techniques in C.

Uploaded by

Nandini Dargude
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 views4 pages

C String Operations: Length, Reverse, Palindrome

The document provides a C program that performs various string operations including calculating the length of a string, reversing a string, checking the equality of two strings, verifying if a string is a palindrome, and checking for a substring. Each operation is demonstrated with code snippets and example outputs. The program showcases fundamental string manipulation techniques in C.

Uploaded by

Nandini Dargude
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

Write a C program that accepts a string from the user and

performs the following string operations- i. Calculate length of


string ii. String reversal iii. Equality check of two
Strings iii. Check palindrome ii. Check substring
- i. Calculate length of string
#include <stdio.h>

int main()

char s[] = "Programming is fun";

int i;

for (i = 0; s[i] != '\0'; ++i);

printf("Length of the string: %d", i);

return 0;

OUTPUT:-

Length of the string:18

ii. String reversal


1. #include <stdio.h>
2. #include <string.h>
3. int main()
4. {
5. char str[40]; // declare the size of character string
6. printf (" \n Enter a string to be reversed: ");
7. scanf ("%s", str);
8.
9. // use strrev() function to reverse a string
10. printf (" \n After the reverse of a string: %s ", strrev(str));
11. return 0;
12. }
13. Output

14. Enter a string to be reversed: AMBULANCE


15. After the reverse of a string: ECNALUBMA

iii. Equality check of two Strings


#include <stdio.h>
#include <string.h>
int main()
{
char str1[] = "Hello";
char str2[] = "World";
int result = strcmp(str1, str2);
if (result == 0)
{
printf("The strings are equal.\n");
}
else if (result < 0)
{
printf("String 1 is less than String 2.\n");
}
else
{
printf("String 1 is greater than String 2.\n");
}
return 0;
}

iv. Check palindrome


// Program to check for a palindrome string by reversing
// the string
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>

char *strrev(char *str) {


int len = strlen(str);

// Temporary char array to store the


// reversed string
char *rev = (char *)malloc
(sizeof(char) * (len + 1));

// Reversing the string


for (int i = 0; i < len; i++) {
rev[i] = str[len - i - 1];
}
rev[len] = '\0';
return rev;
}

void isPalindrome(char *str) {


// Reversing the string
char *rev = strrev(str);

// Check if the original and reversed


// strings are equal
if (strcmp(str, rev) == 0)
printf("\"%s\" is palindrome.\n",
str);
else
printf("\"%s\" is not palindrome.\n",
str);
}

int main() {

// Cheking for palindrome strings


isPalindrome("madam");
isPalindrome("hello");

return 0;
}
Output
"madam" is palindrome.
"hello" is not palindrome.

v. Check substring
#include<stdio.h>
int main()
{
char str[80], substr[10];
int count1 = 0, count2 = 0, i, j, flag;

//user enters the string


printf("Enter a string: ");
scanf("%s", str);
//user enters the substring
printf("Enter a substring to search: ");
scanf("%s", substr);

//'\0' represents the end of the string


while (str[count1] != '\0')
count1++;
while (substr[count2] != '\0')
count2++;
for (i = 0; i <= count1 - count2; i++)
{
for (j = i; j < i + count2; j++)
{
flag = 1;
if (str[j] != substr[j - i])
{
flag = 0;
break;
}
}
if (flag == 1)
break;
}
if (flag == 1)
printf("Substring is found in the entered string");
else
printf("Substring is not found in the entered string");

return 0;
}
Output 1:

You might also like