c语言实现给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初 始顺序。 输入: "Let's take LeetCode contest" 输出: "s'teL ekat edoCteeL tsetnoc"
时间: 2023-05-20 12:01:59 浏览: 110
以下是C语言实现给定一个字符串,反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序的代码:
```c
#include <stdio.h>
#include <string.h>
void reverse(char *s, int start, int end) {
while (start < end) {
char temp = s[start];
s[start++] = s[end];
s[end--] = temp;
}
}
char *reverseWords(char *s) {
int len = strlen(s);
int start = 0, end = 0;
while (end <= len) {
if (s[end] == ' ' || s[end] == '\0') {
reverse(s, start, end - 1);
start = end + 1;
}
end++;
}
return s;
}
int main() {
char s[] = "Let's take LeetCode contest";
printf("%s\n", reverseWords(s));
return 0;
}
```
输出结果为:"s'teL ekat edoCteeL tsetnoc"
相关问题
给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序。用c语言表示出来
以下是一个基于 C 语言的实现:
```c
#include <stdio.h>
#include <string.h>
void reverse(char *s, int start, int end) {
while (start < end) {
char temp = s[start];
s[start] = s[end];
s[end] = temp;
start++;
end--;
}
}
void reverseWords(char *s) {
int start = 0; // 当前单词的起始位置
int end = 0; // 当前单词的结束位置
int len = strlen(s);
while (end <= len) {
if (s[end] == ' ' || s[end] == '\0') {
// 当遇到空格或字符串末尾时,对当前单词进行反转
reverse(s, start, end - 1);
start = end + 1;
}
end++;
}
}
int main() {
char str[] = "hello world";
reverseWords(str);
printf("%s\n", str); // 输出 "olleh dlrow"
return 0;
}
```
该程序中,`reverse` 函数用于反转字符串中某一段的字符顺序,`reverseWords` 函数则用于遍历字符串中的每个单词,并对其进行反转。最终,我们可以得到反转后的字符串。
14、 给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初 始顺序。 输入: "Let's take LeetCode contest" 输出: "s'teL ekat edoCteeL tsetnoc" 请用C语言使用三种方法实现
方法一:使用指针
```c
#include <stdio.h>
#include <string.h>
void reverse(char* start, char* end) {
while (start < end) {
char temp = *start;
*start++ = *end;
*end-- = temp;
}
}
char* reverseWords(char* s) {
char* start = s;
char* end = s;
while (*end != '\0') {
if (*end == ' ') {
reverse(start, end - 1);
start = end + 1;
}
end++;
}
reverse(start, end - 1);
return s;
}
int main() {
char s[] = "Let's take LeetCode contest";
printf("%s\n", reverseWords(s));
return 0;
}
```
方法二:使用数组
```c
#include <stdio.h>
#include <string.h>
void reverse(char s[], int start, int end) {
while (start < end) {
char temp = s[start];
s[start++] = s[end];
s[end--] = temp;
}
}
char* reverseWords(char s[]) {
int len = strlen(s);
int start = 0;
int end = 0;
while (end <= len) {
if (s[end] == ' ' || s[end] == '\0') {
reverse(s, start, end - 1);
start = end + 1;
}
end++;
}
return s;
}
int main() {
char s[] = "Let's take LeetCode contest";
printf("%s\n", reverseWords(s));
return 0;
}
```
方法三:使用递归
```c
#include <stdio.h>
#include <string.h>
void reverse(char s[], int start, int end) {
while (start < end) {
char temp = s[start];
s[start++] = s[end];
s[end--] = temp;
}
}
void reverseWordsHelper(char s[], int start, int end) {
if (start >= end) {
return;
}
int i = start;
int j = start;
while (j <= end) {
if (s[j] == ' ') {
reverse(s, i, j - 1);
i = j + 1;
}
j++;
}
reverse(s, i, j - 1);
reverseWordsHelper(s, start, i - 2);
reverseWordsHelper(s, j, end);
}
char* reverseWords(char s[]) {
int len = strlen(s);
reverseWordsHelper(s, 0, len - 1);
return s;
}
int main() {
char s[] = "Let's take LeetCode contest";
printf("%s\n", reverseWords(s));
return 0;
}
```
阅读全文