cs1713-day1-prog2.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * Copyright (C) 2020, 2019, 2018, 2017 Girish M
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 3 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  16. * MA 02110-1301, USA.
  17. *
  18. */
  19. /*-----------------------------------
  20. Name: Girish M
  21. Roll number: cs1713
  22. Date: 25 July 2017
  23. Program description: Swap the values of two variables. How many ways of swapping do
  24. you know? For each method of swap, determine the pros and cons.
  25. Acknowledgements:http://pubs.opengroup.org/onlinepubs/7908799/xsh/regcomp.html
  26. ------------------------------------*/
  27. #include <stdio.h>
  28. #include <math.h>
  29. #include <ctype.h>
  30. #include <stdlib.h>
  31. #include <regex.h>
  32. int match(const char *string, char *pattern)
  33. {
  34. int status;
  35. regex_t re;
  36. if (regcomp(&re, pattern, REG_EXTENDED|REG_NOSUB) != 0) {
  37. return(0); /* report error */
  38. }
  39. status = regexec(&re, string, (size_t) 0, NULL, 0);
  40. regfree(&re);
  41. if (status != 0) {
  42. return(0); /* report error */
  43. }
  44. return(1);
  45. }
  46. int main(int argc, char* argv[])
  47. {
  48. int num1, num2;
  49. char* pattern = "^[+-]?[0-9]+$";
  50. /*check whether number or not*/
  51. if(argc == 3)
  52. {
  53. if(match(argv[1], pattern) && match(argv[2], pattern))
  54. {
  55. num1 = atoi(argv[1]);
  56. num2 = atoi(argv[2]);
  57. printf("\nBefore swapping integer1: %d integer2: %d\n", num1, num2);
  58. /*swap w/o temporary variable*/
  59. num1 = num1 + num2;
  60. num2 = num1 - num2;
  61. num1 = num1 - num2;
  62. printf("\nAfter swapping integer1: %d integer2: %d\n", num1, num2);
  63. }
  64. else
  65. {
  66. printf("\n./cs1713-day1-prog2.c integer1 integer2\n");
  67. }
  68. }
  69. else
  70. {
  71. printf("\n./cs1713-day1-prog2.c integer1 integer2\n");
  72. }
  73. return 0;
  74. }