Advertisement
insta_guru

💾 String Manipulation functions in C without using Library

Jan 27th, 2019
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.71 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int StringLength(char *);
  4. char *StringReverse(char *);
  5. char *ToLowerCase(char *);
  6. char *ToUpperCase(char *);
  7. char *StringLeft(char *, int);
  8. char *StringRight(char *, int);
  9. char *StringMid(char *, int, int);
  10. char *StringMid2(char *, int);
  11.  
  12. /*
  13.     Some core level string manipulating functions written in C
  14.     Created Date    : [1/26/2019]
  15.     Modified Date   : [1/27/2019]
  16. */
  17. void main() {
  18.  
  19.     printf("%s\n", "String Manipulations In C without using Library");
  20.     printf("%s\n", "===============================================");
  21.     printf("\n");
  22.  
  23.     char MyString[200];
  24.     printf("Enter String: ");
  25.     scanf("%[^\n]s", MyString); // allows all character but NewLine
  26.  
  27.     printf("\n");
  28.     printf("Original : %s\n", MyString);
  29.     printf("Length   : %d\n", StringLength(MyString));
  30.     char* revString = StringReverse(MyString);
  31.     printf("Reversed : %s\n", revString);
  32.     printf("ToLower  : %s\n", ToLowerCase(MyString));
  33.     printf("ToUpper  : %s\n", ToUpperCase(MyString));
  34.     printf("Left(0)  : %s\n", StringLeft(MyString, 0));
  35.     printf("Left(4)  : %s\n", StringLeft(MyString, 4));
  36.     printf("Left(99) : %s\n", StringLeft(MyString, 99));
  37.     printf("Right(0) : %s\n", StringRight(MyString, 0));
  38.     printf("Right(7) : %s\n", StringRight(MyString, 7));
  39.     printf("Right(99): %s\n", StringRight(MyString, 100));
  40.     printf("Mid(3,5) : %s\n", StringMid(MyString, 3, 5));
  41.     printf("Mid(8)   : %s\n", StringMid2(MyString, 8));
  42.     printf("\nEnd of the Program!");
  43. }
  44.  
  45. /* Function to find the length of the given string*/
  46. int StringLength(char *InStr) {
  47.     int i = 0;
  48.     while (InStr[i++] != '\0');
  49.     return --i;
  50. }
  51.  
  52. /* Function that reserve the given string*/
  53. char* StringReverse(char *InStr) {
  54.  
  55.     int len = StringLength(InStr);
  56.  
  57.     //Dynamic memory allocation
  58.     char *OutStr = malloc(sizeof(char) * (len));
  59.  
  60.     int index = 0;
  61.     while (len > 0) {
  62.         OutStr[index++] = InStr[--len];
  63.         //printf_s("%d %d %s\n", len, index, OutStr);
  64.     }
  65.  
  66.     OutStr[index] = '\0';   // Safe Measure
  67.     return OutStr;
  68. }
  69.  
  70. /* Function that converts the given string to lowercase characters*/
  71. char* ToLowerCase(char *InStr) {
  72.  
  73.     int len = StringLength(InStr);
  74.  
  75.     //Dynamic memory allocation
  76.     char *OutStr = malloc(sizeof(char) * (len));
  77.  
  78.     int index = 0;
  79.     while (InStr[index] != '\0') {
  80.         char letter = InStr[index];
  81.         if ((letter >= 'A') && (letter <= 'Z')) // A:65, Z:90
  82.             letter = letter + 32;
  83.         OutStr[index++] = letter;
  84.     }
  85.  
  86.     OutStr[index] = '\0';
  87.     return OutStr;
  88. }
  89.  
  90. /* Function that converts the given string to uppercase characters*/
  91. char* ToUpperCase(char *InStr) {
  92.  
  93.     int len = StringLength(InStr);
  94.  
  95.     //Dynamic memory allocation
  96.     char *OutStr = malloc(sizeof(char) * (len));
  97.  
  98.     int index = 0;
  99.     while (InStr[index] != '\0') {
  100.         char letter = InStr[index];
  101.         if ((letter >= 'a') && (letter <= 'z')) // a:97, z:122
  102.             letter = letter - 32;
  103.         OutStr[index++] = letter;
  104.     }
  105.  
  106.     OutStr[index] = '\0';
  107.     return OutStr;
  108. }
  109.  
  110. /* Function that extracts given N characters from the given string starting from left*/
  111. char* StringLeft(char *InStr, int n) {
  112.  
  113.     if (n <= 0) n = 0;
  114.  
  115.     int len = StringLength(InStr);
  116.     if (n >= len) n = len;
  117.  
  118.     //Dynamic memory allocation
  119.     char *OutStr = malloc(sizeof(char) * (n));
  120.  
  121.     int index = 0;
  122.     while (n > 0) {
  123.         OutStr[index] = InStr[index];
  124.         index++;
  125.         n--;
  126.     }
  127.  
  128.     OutStr[index] = '\0';
  129.     return OutStr;
  130. }
  131.  
  132. /* Function that extracts given N characters from the given string starting from right*/
  133. char* StringRight(char *InStr, int n) {
  134.  
  135.     if (n <= 0) n = 0;
  136.  
  137.     int len = StringLength(InStr);
  138.     if (n >= len) n = len;
  139.  
  140.     //Dynamic memory allocation
  141.     char *OutStr = malloc(sizeof(char) * (n));
  142.  
  143.     int index = 0;
  144.     while (n > 0) {
  145.         OutStr[index] = InStr[len - n];
  146.         index++;
  147.         n--;
  148.     }
  149.  
  150.     OutStr[index] = '\0';
  151.     return OutStr;
  152. }
  153.  
  154. /* Function that extracts given N characters from the given string starting from start position*/
  155. char* StringMid(char *InStr, int start, int n) {
  156.  
  157.     if (n <= 0) n = 0;
  158.     if (start <= 0) start = 1;
  159.  
  160.     int len = StringLength(InStr);
  161.     if (n >= len) n = len;
  162.  
  163.     //Dynamic memory allocation
  164.     char *OutStr = malloc(sizeof(char) * (n));
  165.  
  166.     int index = 0;
  167.     while (n > 0) {
  168.         OutStr[index] = InStr[(start - 1) + index];
  169.         index++;
  170.         n--;
  171.     }
  172.  
  173.     OutStr[index] = '\0';
  174.     return OutStr;
  175. }
  176.  
  177. /* Function that extracts characters from the given string starting from start position till end*/
  178. char* StringMid2(char *InStr, int start) {
  179.  
  180.     int len = StringLength(InStr);
  181.     int n = len - start;
  182.  
  183.     if (start <= 0) {
  184.         start = 1;
  185.         n = len;
  186.     }
  187.  
  188.     //Dynamic memory allocation
  189.     char *OutStr = malloc(sizeof(char) * (n));
  190.  
  191.     int index = 0;
  192.     while (n >= 0) {
  193.         OutStr[index] = InStr[(start - 1) + index];
  194.         //printf_s("%d %d %s\n", len, index, OutStr);
  195.         index++;
  196.         n--;
  197.     }
  198.  
  199.     OutStr[index] = '\0';
  200.     return OutStr;
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement