C Program to Determine if One String is a Circular Permutation of Another String

«
»
This C Program determines if the two entered strings are circular permutation of another string. Eg: yzx is a circular permutation of xyz but not zyx.

Here is a source code of the C program to determine if the two entered string are a circular permutation of one another. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Determine if One String is a Circular Permutation of
  3.  * Another String 
  4.  */
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <ctype.h>
  8. #include <stdlib.h>
  9. #define CHAR_SIZE 26
  10.  
  11. void alphacheck(char *, int []);
  12. void create(char [], char [], int[]);
  13.  
  14. int main()
  15. {
  16.     char str1[50], str2[50];
  17.     int a1[CHAR_SIZE] = {0};
  18.     char str2_rem[50];
  19.  
  20.     printf("Enter string1: ");
  21.     scanf("%s", str1);
  22.     printf("Enter string2: ");
  23.     scanf("%s", str2);
  24.     alphacheck(str1, a1);
  25.     create(str2_rem, str2, a1);
  26.     printf("On removing characters from second string we get: %s\n", str2_rem);
  27.  
  28.     return 0;
  29. }
  30.  
  31. void alphacheck(char *str, int a[])
  32. {
  33.     int i, index;
  34.  
  35.     for (i = 0; i < strlen(str); i++)
  36.     {
  37.         str[i] = tolower(str[i]);
  38.         index = str[i] - 'a';
  39.         if (!a[index])
  40.         {
  41.             a[index] = 1;
  42.         }
  43.     }
  44.     printf("\n");
  45. }
  46.  
  47. void create(char str_rem[], char str[], int list[])
  48. {
  49.     int i, j = 0, index;
  50.  
  51.     for (i = 0; i < strlen(str); i++)
  52.     {
  53.         index = str[i] - 'a';
  54.         if (!list[index])
  55.         {
  56.             str_rem[j++] = str[i];
  57.         }
  58.     }
  59.     str_rem[j] = '\0';
  60. }

advertisement
$ gcc circularpermu.c 
$ ./a.out
Enter string 1: abcd
Enter string 2: dabc
abcd & dabc are circular permutation of each other.

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement

Here’s the list of Best Reference Books in C Programming, Data-Structures and Algorithms

If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn