-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcase-insensitive-pattern.c
69 lines (64 loc) · 1.65 KB
/
case-insensitive-pattern.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
* By Wyatt Ward; 23 Nov 2020. I hereby release this program into the public
* domain. If you really like it, or use it in something then please consider
* reaching out to me/letting me know, if you can. I like to hear from people.
* But there's no pressure and you are certainly not obligated to. It'd just
* be fun for me to hear about what my code is helping with.
*
* Input: text to make case insensitive, either to stdin or as argument(s). If
* multiple arguments are given the result will be printed with spaces as
* separators. If no arguments are passed, then stdin will be read; otherwise,
* if argument(s) are passed, then stdin will _NOT_ be read.
*/
#include <stdio.h> /* putchar/getchar */
#include <ctype.h> /* toupper/tolower */
#include <string.h> /* strlen */
void generate_insensitive(int curchar);
void piperead();
int main(int argc, char **argv)
{
if(argc > 1)
{
for(int i=1; i<argc; i++)
{
for(int j=0; j<strlen(argv[i]); j++)
{
generate_insensitive(argv[i][j]);
}
if((i+1)<argc) /* if not the last argument */
{
putchar(' ');
}
}
putchar('\n');
return 0;
}
else /* no arguments were passed, read from stdin */
{
piperead();
return 0;
}
}
void piperead()
{
int curchar='\0';
while((curchar=getchar()) != EOF)
{
generate_insensitive(curchar);
}
}
/* prints to stdout. Non-alphabetical chars are printed as they were input. */
void generate_insensitive(int curchar)
{
if(isalpha(curchar))
{
putchar((int)'[');
putchar(toupper(curchar));
putchar(tolower(curchar));
putchar((int)']');
}
else
{
putchar(curchar);
}
}