Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Basic Idea
- while(1)
- {
- operarion1 : Palindrome
- operation2 : Sorting
- Operation3 : Analyze String
- Operation4 : Exit
- }
- */
- # include <stdio.h>
- # include <stdlib.h>
- int palindrome_Usman(int n)
- {
- int rev=0, rem, x, i;
- x = n;
- while(x!=0)
- {
- rem = x % 10;
- rev = rev * 10 + rem;
- x = x / 10;
- }
- if(rev == n)
- return n;
- else
- return -1;
- }
- void sort_Usman(int x[], int n)
- {
- int i,j,temp;
- for(i=0; i<n-1; i++) //0 to n-2
- {
- for(j=0; j<n-1; j++)//0 to n-2
- {
- if(x[j] > x[j+1])
- {
- temp = x[j];
- x[j] = x[j+1];
- x[j+1] = temp;
- }
- }
- }
- //Display the sorted array
- for(i=0 ; i<n ; i++)
- {
- printf("%d ",x[i]);
- }
- }
- void character_freq_Usman(int n)
- {
- int i,j,index;
- for(i=1 ; i<=n ; i++)
- {
- int freq[256] = {0};// [0 0 0 0 ..... 0] usmanuu
- char str[100];
- printf("Enter string-%d : ",i);
- scanf("%s",str); //%s, %[^\n]
- for(j=0; str[j]!='\0'; j++)
- {
- index = str[j];
- freq[index]++;
- }
- //1st Task: Unique character print
- printf("Unique Characters: ");
- for(j=0; j<256; j++)
- {
- if(freq[j] == 1)
- {
- printf("%c ",j);
- }
- }
- printf("\n");
- //2nd Task: Duplicate character & their frequency print
- printf("Duplicate Characters:\n");
- for(j=0; j<256; j++)
- {
- if(freq[j] > 1)
- {
- printf("%c %d\n",j,freq[j]);
- }
- }
- printf("\n");
- }
- }
- int main()
- {
- int choice,lowest,highest,n,i,result;
- while(1)
- {
- printf("-----------Menu Driven Interface-----------\n");
- printf("Select 1 to Find Palindrome Numbers in a Given Range\n");
- printf("Select 2 to Sort an Array of Random Numbers\n");
- printf("Select 3 to Analyze Strings for Character Properties\n");
- printf("Select 4 to Exit the Program\n");
- printf("Select your Choice (1 to 4): ");
- scanf("%d", &choice);
- switch(choice)
- {
- case 1:
- printf("Enter the lowest value of the range: ");
- scanf("%d",&lowest);
- printf("Enter the highest value of the range: ");
- scanf("%d",&highest);
- for(i=lowest; i<=highest; i++)
- {
- result = palindrome_Usman(i);
- if(result == i)
- {
- printf("%d ",result);
- }
- }
- printf("\n");
- break;
- case 2:
- printf("Enter the number of Elements: ");
- scanf("%d",&n);
- int arr[200];
- for(i=0;i<n;i++)
- {
- arr[i] = rand() % 101;
- }
- sort_Usman(arr,n);
- printf("\n");
- break;
- case 3:
- printf("Enter the number of String you want to take as input: ");
- scanf("%d", &n);
- character_freq_Usman(n);
- break;
- case 4: // you can write case 4 instead
- printf("Thank You, Program Ended!");
- return 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement