NightRaven97

String manipulation without using lib funcs

Feb 10th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.98 KB | None | 0 0
  1. #include<stdio.h>
  2. int len(char *x)
  3. {
  4.     int i=0;
  5.     while(x[i]!='\0')
  6.         i++;
  7.     return i;
  8. }
  9. char * cpy(char *x, char *y)
  10. {
  11.     int i=0,l=len(y);
  12.     while(i<l)
  13.     {
  14.         x[i]=y[i]; 
  15.         i++;   
  16.     }
  17.     x[i]='\0';
  18.     return x;
  19. }
  20. char *cat(char *x, char* y)
  21. {
  22.     int i=len(x),j=0,l=len(y);
  23.     while(j<l)
  24.     {
  25.         x[i]=y[j];
  26.         i++;
  27.         j++;
  28.     }
  29.     x[i]='\0';
  30.     return x;
  31. }
  32. int cmp(char *x, char*y)
  33. {
  34.     int l1=len(x),l2=len(y),i=0,temp;
  35.     temp=(l1<l2)?l1:l2;
  36.     while(i<temp)
  37.     {
  38.         if(x[i]==y[i])
  39.             ;
  40.         else if(x[i]>y[i])
  41.             return 1;
  42.         else
  43.             return -1;
  44.         i++;
  45.     }
  46.     if(x[i]!='\0')
  47.         return 1;
  48.     else if(y[i]!='\0')
  49.         return -1;
  50.     else
  51.         return 0;
  52. }
  53. int cmpi(char *x, char*y)
  54. {
  55.     int l1=len(x),l2=len(y),i=0,temp;
  56.     temp=(l1<l2)?l1:l2;
  57.     while(i<temp)
  58.     {
  59.         if(x[i]==y[i] || x[i]+32==y[i] || x[i]==y[i]+32)
  60.             ;
  61.         else if(x[i]>y[i])
  62.             return 1;
  63.         else
  64.             return -1;
  65.         i++;
  66.     }
  67.     if(x[i]!='\0')
  68.         return 1;
  69.     else if(y[i]!='\0')
  70.         return -1;
  71.     else
  72.         return 0;
  73. }
  74. char *rev(char *x)
  75. {
  76.     int i=0,j=len(x)-1;
  77.     char temp;
  78.     while(i<len(x)/2)
  79.     {
  80.         temp=x[i];
  81.         x[i]=x[j];
  82.         x[j]=temp;
  83.         i++;
  84.         j--;
  85.     }
  86.     return x;
  87. }
  88. char *lwr(char *x)
  89. {
  90.     int i,l=len(x);
  91.     for(i=0;i<l;i++)
  92.     {
  93.         if(x[i]>=65 && x[i]<=90)
  94.             x[i]+=32;
  95.     }
  96.     return x;
  97. }
  98. char *upr(char *x)
  99. {
  100.     int i,l=len(x);
  101.     for(i=0;i<l;i++)
  102.     {
  103.         if(x[i]>=97 && x[i]<=122)
  104.             x[i]-=32;
  105.     }
  106.     return x;
  107. }
  108. void main()
  109. {
  110.     char str1[100],str2[100];
  111.     printf("STRING MANIPULATION WITHOUT USING LIBRARY FUNCTIONS\n");
  112.     int ch1=0,ch2,flag=0,temp;
  113.     while(ch1<1 || ch1>6)
  114.     {
  115.         if(flag)
  116.             printf("Invalid input!");
  117.         printf("1.Length of string\n2.Copy a string\n3.Concatenate two strings\n4.Compare two strings\n5.Reverse a string\n6.Case conversion\nEnter your choice:");
  118.         scanf("%d",&ch1);
  119.         flag++;
  120.     }
  121.     switch(ch1)
  122.     {
  123.         case 1:
  124.             printf("Enter a string:");
  125.             scanf("%s",&str1);
  126.             printf("Length of %s is %d.",str1,len(str1));
  127.             break;
  128.         case 2:
  129.             printf("Enter source string and target string:");
  130.             scanf("%s %s",&str1,&str2);
  131.             cpy(str1,str2);
  132.             printf("Contents of str2 after copying:%s",str2);
  133.             break;
  134.         case 3:
  135.             printf("Enter two strings:");
  136.             scanf("%s %s",&str1,&str2);
  137.             printf("Before concatenation:\nstr1:%s\tstr2:%s",str1,str2);
  138.             cat(str1,str2);
  139.             printf("\nAfter concatenation:\nstr1:%s\tstr2:%s",str1,str2);
  140.             break;
  141.         case 4:
  142.             flag=0;
  143.             ch2=0;
  144.             while(ch2!= 1 && ch2!=2)
  145.             {
  146.                 if(flag)
  147.                     printf("Invalid input!\n");
  148.                 printf("1.Case sensitive comparison\n2.Case insensitive comparison\nEnter your choice:");
  149.                 scanf("%d",&ch2);
  150.                 flag++;
  151.             }
  152.             switch(ch2)
  153.             {
  154.                 case 1:
  155.                     printf("Enter two strings:");
  156.                     scanf("%s %s",&str1,&str2);
  157.                     temp=cmp(str1,str2);
  158.                     printf("Result of comparison:%d\n",temp);
  159.                     if(temp==0)
  160.                         printf("The strings are equal.");
  161.                     else
  162.                         printf("The strings are not equal.");
  163.                     break;
  164.                 case 2:
  165.                     printf("Enter two strings:");
  166.                     scanf("%s %s",&str1,&str2);
  167.                     temp=cmpi(str1,str2);
  168.                     printf("Result of comparison:%d\n",temp);
  169.                     if(temp==0)
  170.                         printf("The strings are equal.");
  171.                     else
  172.                         printf("The strings are not equal.");
  173.                     break;
  174.             }
  175.             break;
  176.         case 5:
  177.             printf("Enter a string:");
  178.             scanf("%s",str1);
  179.             printf("Reverse of %s is ",str1);
  180.             printf("%s",rev(str1));
  181.             break;
  182.         case 6:
  183.             flag=0;
  184.             ch2=0;
  185.             while(ch2!=1 && ch2!=2)
  186.             {
  187.                 if(flag)
  188.                     printf("Invalid input!\n");
  189.                 printf("1.Upper case to lower case\n2.Lower case to upper case\nEnter your chocie:");
  190.                 scanf("%d",&ch2);
  191.                 flag++;
  192.             }
  193.             switch(ch2)
  194.             {
  195.                 case 1:
  196.                     printf("Enter the string:");
  197.                     scanf("%s",&str1);
  198.                     printf("str1 before case-conversion:%s",str1);
  199.                     lwr(str1);
  200.                     printf("\nstr1 after case-conversion:%s",str1);
  201.                     break;
  202.                 case 2:
  203.                     printf("Enter the string:");
  204.                     scanf("%s",&str1);
  205.                     printf("str1 before case-conversion:%s",str1);
  206.                     upr(str1);
  207.                     printf("\nstr1 after case-conversion:%s",str1);
  208.                     break;
  209.             }
  210.             break;
  211.     }
  212. }
Add Comment
Please, Sign In to add comment