Wednesday, 10 July 2019

C program for operation on String

Implement following operations on the string (without library string)
1. Length 2. Palindrome 3. Comparison 4. Copy 5. Reverse      6. Substring

#include <stdio.h>

void substr(char str1[], char str2[]);
int palindrome(char str[]);
int compare(char str1[], char str2[]);
void copy(char str1[], char str2[]);
void reverse(char str[]);
int main()
{
      setbuf(stdout, NULL);
      char str1[100], str2[100];
      int result, option;
      do {
            printf("\n1.Substring Searching");
            printf("\n2.Check for Palindrome");
            printf("\n3.String Comparison");
            printf("\n4.Copy string");
            printf("\n5.Reverse String");
            printf("\n6.Quit");
            printf("\n\nEnter Your Choice:");
            scanf("%d", &option);
            switch (option)
            {
            case 1:
                  printf("\nEnter 1st string:");
                  scanf("%s",&str1);
                  printf("\nEnter 2nd string:");
                  scanf("%s",&str2);
                  substr(str1, str2);

                  break;
            case 2:
                  printf("\nEnter a String:");
                  scanf("%s",&str1);
                  result = palindrome(str1);
                  if (result == 0)
                        printf("\nNot a palindrome");
                  else
                        printf("\nIt is palindrome");

                  break;
            case 3:
                  printf("\nEnter 1st string:");
                  scanf("%s",&str1);
                  printf("\nEnter 2nd string:");
                  scanf("%s",&str2);
                  result = compare(str1, str2);
                  if (result == 0)
                        printf("\nboth are same");
                  else if (result > 0)
                        printf("\n1st>2nd");
                  else
                        printf("\n1st<2nd");

                  break;
                  case 4:
                        printf("\nEnter a String:");
                        scanf("%s",&str1);
                        copy(str2, str1);
                        printf("\nResult=%s", str2);

                        break;
                  case 5:
                        printf("\nEnter a String:");scanf("%s",&str1);
                        reverse(str1);
                        printf("\nResult=%s", str1);

                        break;
                  default:
                        printf("\nInvalid Choice:");
                        break;
            }
      } while (option != 6);
      return 0;
}

void substr(char str1[], char str2[])
{
      int i, j, lena, lenb;
      for (lena = 0; str1[lena] != '\0'; lena++);
      for (lenb = 0; str2[lenb] != '\0'; lenb++);
      for (i = 0; i <= lena - lenb + 1; i++) {
            for (j = 0; str1[i + j] == str2[j] && str2[j] != '\0'; j++);
            if (str2[j] == '\0')
                  printf("\nString found at location : %d", i + 1);
      }
}

int palindrome(char str[])
{
      int i, j;
      i = j = 0;
      while (str[j] != '\0')
            j++;
      while (i < j) {
            if (str[i] != str[j - 1])
                  return (0);
            i++;
            j--;
      }
      return (1);
}

int compare(char str1[], char str2[])
{
      int i;
      i = 0;
      while (str1[i] != '\0') {
            if (str1[i] > str2[i])
                  return (1);
            if (str1[i] < str2[i])
                  return (-1);
            i++;
      }
      return (0);
}

void copy(char str2[], char str1[])
{
      int i = 0;
      while (str1[i] != '\0') {
            str2[i] = str1[i];
            i++;
      }
      str2[i] = '\0';
}

void reverse(char str[])
{
      int i, j;
      char temp;
      i = j = 0;
      while (str[j] != '\0')
            j++;
      j--;
      while (i < j) {
            temp = str[i];
            str[i] = str[j];
            str[j] = temp;
            i++;
            j--;
      }
}

Output
1.Substring Searching
2.Check for Palindrome
3.String Comparison
4.Copy string
5.Reverse String
6.Quit

Enter Your Choice:1

Enter 1st string:madam

Enter 2nd string:am

String found at location : 4
1.Substring Searching
2.Check for Palindrome
3.String Comparison
4.Copy string
5.Reverse String
6.Quit

Enter Your Choice:2

 Enter a String:madam

A palindrome
1.Substring Searching
2.Check for Palindrome
3.String Comparison
4.Copy string
5.Reverse String
6.Quit

Enter Your Choice:3

Enter 1st string:Mam

Enter 2nd string:mam

1st<2nd
1.Substring Searching
2.Check for Palindrome
3.String Comparison
4.Copy string
5.Reverse String
6.Quit

Enter Your Choice:4

Enter a String:hello

Result=hello
1.Substring Searching
2.Check for Palindrome
3.String Comparison
4.Copy string
5.Reverse String
6.Quit

Enter Your Choice:5

Enter a String:hello

Result=olleh
1.Substring Searching
2.Check for Palindrome
3.String Comparison
4.Copy string
5.Reverse String
6.Quit

Enter Your Choice:6

Invalid Choice:

No comments:

Post a Comment

Stack and Queue as ADT in C++

Stack as ADT The functions associated with stack are: empty()  – Returns whether the stack is empty size()  – Returns the size o...