Advertisement
Mehadi_Hasan_Santo

Menu_Creation_Usman

May 31st, 2025
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.60 KB | None | 0 0
  1. /*
  2. Basic Idea
  3. while(1)
  4. {
  5.     operarion1 : Palindrome
  6.     operation2 : Sorting
  7.     Operation3 : Analyze String
  8.     Operation4 : Exit
  9.    
  10. }
  11. */
  12.  
  13. # include <stdio.h>
  14. # include <stdlib.h>
  15.  
  16. int palindrome_Usman(int n)
  17. {
  18.     int rev=0, rem, x, i;
  19.     x = n;
  20.     while(x!=0)
  21.     {
  22.         rem = x % 10;
  23.         rev = rev * 10 + rem;
  24.         x = x / 10;
  25.     }
  26.     if(rev == n)
  27.         return n;
  28.     else
  29.         return -1;
  30. }
  31.  
  32.  
  33. void sort_Usman(int x[], int n)
  34. {
  35.     int i,j,temp;
  36.     for(i=0; i<n-1; i++) //0 to n-2
  37.     {
  38.         for(j=0; j<n-1; j++)//0 to n-2
  39.         {
  40.             if(x[j] > x[j+1])
  41.             {
  42.               temp = x[j];
  43.               x[j] = x[j+1];
  44.               x[j+1] = temp;
  45.             }
  46.         }
  47.     }
  48.    
  49.     //Display the sorted array
  50.     for(i=0 ; i<n ; i++)
  51.     {
  52.         printf("%d ",x[i]);
  53.     }
  54. }
  55.  
  56. void character_freq_Usman(int n)
  57. {
  58.     int i,j,index;
  59.     for(i=1 ; i<=n ; i++)
  60.     {
  61.         int freq[256] = {0};// [0 0 0 0 ..... 0] usmanuu
  62.         char str[100];
  63.         printf("Enter string-%d : ",i);
  64.         scanf("%s",str); //%s, %[^\n]
  65.         for(j=0; str[j]!='\0'; j++)
  66.         {
  67.             index = str[j];
  68.             freq[index]++;
  69.         }
  70.         //1st Task: Unique character print
  71.         printf("Unique Characters: ");
  72.         for(j=0; j<256; j++)
  73.         {
  74.             if(freq[j] == 1)
  75.             {
  76.                 printf("%c ",j);
  77.             }
  78.         }
  79.         printf("\n");
  80.        
  81.         //2nd Task: Duplicate character  & their frequency print
  82.         printf("Duplicate Characters:\n");
  83.         for(j=0; j<256; j++)
  84.         {
  85.             if(freq[j] > 1)
  86.             {
  87.                 printf("%c %d\n",j,freq[j]);
  88.             }
  89.         }
  90.         printf("\n");
  91.     }
  92.  
  93. }
  94.  
  95.  
  96. int main()
  97. {
  98.     int choice,lowest,highest,n,i,result;
  99.    
  100.     while(1)
  101.     {
  102.         printf("-----------Menu Driven Interface-----------\n");
  103.         printf("Select 1 to Find Palindrome Numbers in a Given Range\n");
  104.         printf("Select 2 to Sort an Array of Random Numbers\n");
  105.         printf("Select 3 to Analyze Strings for Character Properties\n");
  106.         printf("Select 4 to Exit the Program\n");
  107.        
  108.         printf("Select your Choice (1 to 4): ");
  109.         scanf("%d", &choice);
  110.        
  111.         switch(choice)
  112.         {
  113.             case 1:
  114.                  printf("Enter the lowest value of the range: ");
  115.                  scanf("%d",&lowest);
  116.                  printf("Enter the highest value of the range: ");
  117.                  scanf("%d",&highest);
  118.                  for(i=lowest; i<=highest; i++)
  119.                  {
  120.                     result = palindrome_Usman(i);
  121.                     if(result == i)
  122.                     {
  123.                         printf("%d ",result);
  124.                     }
  125.                  }
  126.                  printf("\n");
  127.                  break;
  128.              
  129.             case 2:
  130.                  printf("Enter the number of Elements: ");
  131.                  scanf("%d",&n);
  132.                  int arr[200];
  133.                  for(i=0;i<n;i++)
  134.                  {
  135.                     arr[i] = rand() % 101;
  136.                  }
  137.                  sort_Usman(arr,n);
  138.                  printf("\n");
  139.                  break;
  140.              
  141.             case 3:
  142.                  printf("Enter the number of String you want to take as input: ");
  143.                  scanf("%d", &n);
  144.                  character_freq_Usman(n);
  145.                  break;
  146.              
  147.             case 4: // you can write case 4 instead
  148.                  printf("Thank You, Program Ended!");
  149.                  return 0;
  150.         }
  151.    
  152.     }
  153.    
  154. }
  155.  
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement