aboutsummaryrefslogtreecommitdiff
path: root/reverse_prefix_word.c
diff options
context:
space:
mode:
Diffstat (limited to 'reverse_prefix_word.c')
-rw-r--r--reverse_prefix_word.c33
1 files changed, 33 insertions, 0 deletions
diff --git a/reverse_prefix_word.c b/reverse_prefix_word.c
new file mode 100644
index 0000000..079d9fe
--- /dev/null
+++ b/reverse_prefix_word.c
@@ -0,0 +1,33 @@
+// 2000. Reverse Prefix of Word
+#include "leetcode.h"
+
+/*
+ * given a 0-indexed string 'word' and a character 'ch', reverse the segment of
+ * 'word' that starts at index 0 and ends at the index of the first occurence of
+ * 'ch' (inclusively). if the character 'ch' does not exist in 'word', do
+ * nothing. return the resulting string.
+ */
+
+char *reversePrefix(char *word, char ch) {
+ int n = strlen(word), loc = -1;
+ for (int i = 0; i < n; i++)
+ if (word[i] == ch) {
+ loc = i;
+ break;
+ }
+ if (loc == -1)
+ return word;
+ for (int i = 0; i <= loc / 2; i++) {
+ char tmp = word[i];
+ word[i] = word[loc - i];
+ word[loc - i] = tmp;
+ }
+ return word;
+}
+
+int main() {
+ char *w1 = "abcdefd", *w2 = "xyxzxe", *w3 = "abcd";
+ printf("%s\n", reversePrefix(w1, 'd')); // expect: dcbaefd
+ printf("%s\n", reversePrefix(w2, 'z')); // expect: zxyxxe
+ printf("%s\n", reversePrefix(w3, 'z')); // expect: abcd
+}