-
Notifications
You must be signed in to change notification settings - Fork 0
/
word.c
107 lines (77 loc) · 1.4 KB
/
word.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
* word.c
*
*
* Created by Alden Walker on 1/28/11.
* Copyright 2011 Caltech. All rights reserved.
*
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "word.h"
char swapCaseChar(char c) {
return (char)((int)c > 96 ? (int)c-32 : (int)c+32);
}
void swapCase(char* s) {
int i;
int sl = strlen(s);
for (i=0; i<sl; i++) {
s[i] = (char)(s[i] > 96 ? (int)s[i]-32 : (int)s[i]+32);
}
}
void invert(char* s) {
int i;
char temp;
int sl = strlen(s);
for (i=0; i<sl/2; i++) {
temp = s[i];
s[i] = s[sl-i-1];
s[sl-i-1] = temp;
}
swapCase(s);
}
void red(char* s) { // reduce by cancelling adjacent inverse letters
int i,a,b;
i=0;
int j;
while(i<=(int)strlen(s)){
if(i>0){
a = (int) s[i-1];
b = (int) s[i];
if((32+a-b)%64==0){
// cout << (*s) << " " << i << "\n";
for (j=i-1; j<strlen(s)-1; j++) {
s[j] = s[j+2];
}
// cout << (*s) << "\n";
i=i-2;
}
}
i++;
}
return;
}
void cyc_red(char* s) {
int sl = strlen(s);
int i;
while (1) {
if (sl == 0) {
return;
}
if (s[0] != swapCaseChar(s[sl-1])) {
return;
}
//remove the first and last
for (i=0; i<sl; i++) {
s[i] = s[i+1];
}
s[sl-1] = '\0';
sl -= 2;
}
}
//string multiply_words(string& s1, string& s2) {
// string ans = s1+s2;
// red(ans);
// return ans;
//}