Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** Exercise 3-3 from K&R The C programming language Book */
- #include <stdio.h>
- #include <ctype.h>
- #define MAX_LENGTH 1000
- void expand(char s1[], char s2[]);
- int main()
- {
- char to[MAX_LENGTH];
- for (int i = 0; i < MAX_LENGTH; i++)
- {
- to[i] = 0;
- }
- char from[] = "---a-b-z-A-Z0-9-----";
- expand(from, to);
- printf("%s\n", to);
- }
- void expand(char s1[], char s2[])
- {
- char previous_char = '\0';
- int j = 0;
- for (int i = 0; s1[i] != '\0'; i++)
- {
- char next_char = s1[i + 1];
- if (s1[i] == '-' && previous_char != '\0' && next_char != '\0' && previous_char < next_char && isalnum(previous_char) && isalnum(next_char))
- {
- while(previous_char < next_char - 1)
- {
- s2[j++] = ++previous_char;
- }
- previous_char = '\0';
- } else {
- s2[j++] = s1[i];
- previous_char = s1[i];
- }
- }
- s2[j] = '\0';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement