#include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <string.h> void rev_word(char line[]); int main(void) { char
*in_buf = NULL; size_t buf_len = 0; ssize_t str_len; if
((str_len = getline(&in_buf, &buf_len, stdin)) != -1) { rev_word(in_buf); printf("%s", in_buf); } /* * free buffer allocated by getline() */ free(in_buf); in_buf = NULL; return(EXIT_SUCCESS); } /* * rev_word * *
Reverses the characters of each word in a string in place *
All whitespace is preserved! * * library use: isspace() to test if char is
whitepsace * * input:
text string * return:
none */ string reverses(string
str) { // Mark spaces in result int n =
str.size(); string result(n, '\0'); for (int i = 0; i < n; i++) if
(str[i] == ' ') result[i] = ' '; // Traverse input string from beginning // and put characters in result from end int j = n
- 1; for (int i = 0; i < str.length(); i++) { // Ignore spaces in input string if
(str[i] != ' ') { // ignore spaces in result. if
(result[j] == ' ') j--; result[j] = str[i]; j--; } } } return result; void rev_word(char
line[]) { return; }
Get Free Quote!
356 Experts Online