-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy path12_reverse_word_wise.c
115 lines (92 loc) · 2.64 KB
/
12_reverse_word_wise.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// // Write a function to reverse a string word wise. (For example if the given string is
// // “Programming is Love” then the resulting string should be “Love is Programming" )
// // Header Files
#include <stdio.h>
#include <conio.h>
#include <string.h>
#define ARRAY_SIZE 31
// // Function Declarations
int strLength(char[]);
char *strReverse(char[]);
char *strReverseWordWise(char[]);
// // Main Function Start
int main()
{
char str[ARRAY_SIZE];
printf("\nEnter Any String to Reverse it Word Wise (MAX CHARACTERS %d) => ", ARRAY_SIZE - 1);
fgets(str, ARRAY_SIZE, stdin); // // Input String
str[strcspn(str, "\n")] = '\0'; // // Replace '\n' character with '\0' in str
printf("\nString Before Reversing => %s", str);
printf("\nString After Reversing => %s", strReverseWordWise(str));
putch('\n');
getch();
return 0;
}
// // Main Function End
// // Function Definitions 👇👇
// // Function to Calculate Length of String
int strLength(char str[])
{
int length = 0;
while (str[length])
length++;
return length;
}
// // Function to Reverse a String
char *strReverse(char str[])
{
int length = strLength(str);
char temp;
// // Reverse str
int beg = 0, end = length - 1;
while (beg < end)
{
// // Swap str[beg] with str[end]
temp = str[beg];
str[beg] = str[end];
str[end] = temp;
beg++;
end--;
}
return str;
}
// // Function to Check Whether a Given String is Palindrome or Not
int isStrPalindrome(char str[])
{
char copyStr[strLength(str) + 1]; // // create a string to copy str
copyString(copyStr, str); // // copy str into copyStr
strReverse(copyStr); // // reverse copyStr
if (compareStrings(copyStr, str))
return 0; // // String is not Palindrome
return 1; // // String is Palindrome
}
// // Function to Reverse a String word wise
char *strReverseWordWise(char str[])
{
strReverse(str); // // Reverse str
int index = 0, lock = 1;
for (int i = 0; str[i]; i++)
{
if (lock && str[i] != 32 && str[i] != '\t')
{
index = i;
lock = 0;
}
else if (lock == 0 && (str[i] == 32 || str[i] == '\t' || str[i + 1] == '\0'))
{
int beg = index, end = str[i + 1] ? i - 1 : i;
char temp;
while (beg < end)
{
// // Swap str[beg] with str[end]
temp = str[beg];
str[beg] = str[end];
str[end] = temp;
beg++;
end--;
}
lock = 1;
}
}
return str;
}