[英]How to Reverse a sentence word by word?
I was trying to reverse a sentence word by word.我试图一个字一个字地颠倒一个句子。 (how are you -> you are how) First of all I create a char sentence and reverse and temp.
(你好吗 -> 你好吗)首先,我创建了一个 char 语句并反向和临时。 Sentence given by user to reverse.
用户给出的要反转的句子。 Temp catches the word to change the location in the sentence.Then I use strcat to concatenate each word.
Temp 捕捉单词改变句子中的位置。然后我使用 strcat 连接每个单词。 Here is the problem.
这是问题所在。 I can find the word which is end of the sent(takes input) but when I'm trying to concatenate to reverse, it add this word to sentence and an error occurs.
我可以找到已发送(接受输入)结尾的单词,但是当我尝试连接以反转时,它会将这个单词添加到句子中并发生错误。 What's the problem?
有什么问题?
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* subs(char* temp, char* src, int start, int end);
int main() {
char sent[15]; //input sentence
char rev[15]; // output sentence
char *temp=(char*)calloc(1,sizeof(char)); //for the word
scanf(" %[^\n]%*c", &sent); // takin' input
int i, end, start;
i = strlen(sent);
//find the beggining and ending of the indexes of the word in sentence
while (i > 0) {
while (sent[i] == ' ') {
i--;
}
end = i-1;
while (sent[i] != ' ') {
i--;
}
start = i + 1;
//add the word to temp and concatenate to reverse
temp=subs(temp, sent, start, end);
strncat(rev, temp,end-start+3);
}
rev[strlen(sent)] = '\0';
printf("%s", rev);
return 0;
}
char* subs(char* temp, char* src, int start, int end) {
int i = 0, control;
// resize the temp for the wırd
temp = (char*)realloc(temp,end-start+3);
for (; i < (end - start) + 1; i++) {
control = (start + i);
temp[i] = src[control];
}
//adding blank and null character to end of the word.
temp[i] = ' ';
temp[++i] = '\0';
return temp;
}
I will just copy my good answer from this question that was not yet closed Reverse a string without strtok in C .我将从这个尚未关闭的问题中复制我的好答案在 C 中反转没有 strtok 的字符串。 So I can not use this reference to close your question as a duplicate.
所以我不能使用这个参考来结束你的问题作为重复。
A standard approach is to reverse each word within a string and then to reverse the whole string.标准方法是反转字符串中的每个单词,然后反转整个字符串。
The standard C function strtok
is not appropriate in this case.在这种情况下,标准 C 函数
strtok
不合适。 Instead use the standard C functions strspn
and strcspn
.而是使用标准的 C 函数
strspn
和strcspn
。
Here you are.这个给你。
#include <stdio.h>
#include <string.h>
static char * reverse( char *s, size_t n )
{
for ( size_t i = 0; i < n / 2; i++ )
{
char c = s[ i ];
s[ i ] = s[ n - i - 1 ];
s[ n - i - 1 ] = c;
}
return s;
}
char * reverse_by_words( char *s )
{
const char *delim = " \t";
char *p = s;
while ( *p )
{
p += strspn( p, delim );
if ( *p )
{
char *q = p;
p += strcspn( p, delim );
reverse( q, p - q );
}
}
return reverse( s, p - s );
}
int main(void)
{
char s[] = "5 60 +";
puts( s );
puts( reverse_by_words( s ) );
return 0;
}
The program output is程序输出是
5 60 +
+ 60 5
If you want to keep leading and trailing spaces as they were in the original string then the functions can look the following way如果您想保留原始字符串中的前导和尾随空格,则函数可以如下所示
#include <stdio.h>
#include <string.h>
static char *reverse( char *s, size_t n )
{
for ( size_t i = 0; i < n / 2; i++ )
{
char c = s[i];
s[i] = s[n - i -1 ];
s[n - i - 1] = c;
}
return s;
}
char * reverse_by_words( char *s )
{
const char *delim = " \t";
char *first = s, *last = s;
for ( char *p = s; *p; )
{
p += strspn( p, delim );
if ( last == s ) first = last = p;
if ( *p )
{
char *q = p;
p += strcspn( p, delim );
last = p;
reverse( q, p - q );
}
}
reverse( first, last - first );
return s;
}
int main(void)
{
char s[] = "\t\t\t5 60 +";
printf( "\"%s\"\n", s );
printf( "\"%s\"\n", reverse_by_words( s ) );
return 0;
}
The program output is程序输出是
" 5 60 +"
" + 60 5"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.