Advertisement
Guest User

Untitled

a guest
Jul 21st, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. /** Exercise 3-3 from K&R The C programming language Book */
  2.  
  3. #include <stdio.h>
  4. #include <ctype.h>
  5.  
  6. #define MAX_LENGTH 1000
  7.  
  8. void expand(char s1[], char s2[]);
  9.  
  10. int main()
  11. {
  12. char to[MAX_LENGTH];
  13. for (int i = 0; i < MAX_LENGTH; i++)
  14. {
  15. to[i] = 0;
  16. }
  17. char from[] = "---a-b-z-A-Z0-9-----";
  18. expand(from, to);
  19. printf("%s\n", to);
  20. }
  21.  
  22. void expand(char s1[], char s2[])
  23. {
  24. char previous_char = '\0';
  25. int j = 0;
  26. for (int i = 0; s1[i] != '\0'; i++)
  27. {
  28. char next_char = s1[i + 1];
  29. if (s1[i] == '-' && previous_char != '\0' && next_char != '\0' && previous_char < next_char && isalnum(previous_char) && isalnum(next_char))
  30. {
  31. while(previous_char < next_char - 1)
  32. {
  33. s2[j++] = ++previous_char;
  34. }
  35. previous_char = '\0';
  36. } else {
  37. s2[j++] = s1[i];
  38. previous_char = s1[i];
  39. }
  40. }
  41. s2[j] = '\0';
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement