C - 字符串数组中单词的倒序
C - Reverse order of words in an array of Strings
我做了这个程序来颠倒给定字符串中单词的顺序。 (而且有效)
即输出:sentence first the is This
但是,在向数组中添加另一个句子时,我遇到了困难。
例如,我需要一个数组 {"This is the first sentence", "And this is the second"}
作为输出生成:sentence first the is This , second the is this And
int main() {
char str[] = {"This is the first sentence"};
int length = strlen(str);
// Traverse string from end
int i;
for (i = length - 1; i >= 0; i--) {
if (str[i] == ' ') {
// putting the NULL character at the position of space characters for
next iteration.
str[i] = '[=10=]';
// Start from next character
printf("%s ", &(str[i]) + 1);
}
}
// printing the last word
printf("%s", str);
return 0;
}
我是 C 的新手,所以即使解决方案很简单,我还是卡住了也就不足为奇了。任何帮助,将不胜感激!谢谢!
因为您已经有了以相反顺序打印一个字符串的单词的代码,我建议制作一个 函数,它接受一个字符串作为参数,即:
void print_words_reverse(char * const str) {
// your current code here
}
然后你可以为每个字符串单独调用它:
char strings[][30] = {
"This is the first sentence",
"And this is the second"
};
for (int i = 0; i < sizeof(strings) / sizeof(*strings); ++i) {
print_words_reverse(strings[i]);
}
请注意,由于您正在修改字符串(通过用 NUL 字节替换空格),参数需要是可修改的,这意味着您不能(在标准 C 中)使用指向字符串文字的指针来调用它,这意味着您不能简单地使用 const char *strings[] = { "first", "second" }
。您可以通过让您的代码不修改参数字符串来摆脱为每个字符串保留的难看的常量长度(此处为 30
)。或者您可以为每个句子使用一个单独的字符数组,然后使用指向那些(可修改的)字符串的指针。
首先,您可以尝试使用二维数组或使用指针数组。
其次,在你的方法中,你丢失了字符串的初始值,我不知道它有多重要。
这是我使用指针数组的快速方法。
#include <stdio.h>
#include <string.h>
static void print_word(const char *str)
{
for (int i = 0; str[i] && str[i] != ' '; i++)
printf("%c", str[i]);
putchar(' ');
}
int main(void)
{
int len;
const char *str[] = {"This is the first sentence",
"And this is second", NULL};
for (int i = 0; str[i]; i++) {
for (len = strlen(str[i]); len >= 0; len--) {
if (len == 0)
print_word(&str[i][len]);
else if (str[i][len] == ' ')
print_word(&str[i][len + 1]);
}
putchar('\n');
}
printf("Initial value of array of strings [%s | %s] \n", str[0], str[1]);
return 0;
}
输出为:
sentence first the is This
second is this And
Initial value of array of strings [This is the first sentence | And this is second]
我建议您使用 memcpy
,但无需过多更改您的代码,这似乎可行
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX_STRING_LENGTH 100
int main()
{
char *str[] = {"This is the first", "And this is the second sentence"};
const size_t NUM_STRING = sizeof(str)/sizeof(char*);
/*%z used to print size_t variables*/
printf("%zd strings found\n", NUM_STRING);
int length[2];
int i;
for (i=0; i<NUM_STRING; i++)
{
length[i] = strlen(str[i]);
}
printf("length initialized %d %d\n", length[0], length[1]);
// Traverse string from end
int j = 0;
char temp[MAX_STRING_LENGTH];
printf("\n\n");
for (j=0; j<NUM_STRING; j++)
{
/*Make sure the string respect the MAX_STRING_LENGTH limit*/
if (strlen(str[j])>MAX_STRING_LENGTH)
{
printf("ERROR: string %d exceding max string length %d defined in constant "
"MAX_STRING_LENGTH. Exiting from program.\n", j, MAX_STRING_LENGTH);
exit(1);
}
//reset temporary string
memset(temp, '[=10=]', sizeof(temp));
//printf("temp variable reinitialized\n");
for (i = length[j] - 1; i >= 0; i--)
{
temp[i] = str[j][i];
if (str[j][i] == ' ')
{
// putting the NULL character at the position of space characters for next iteration.
temp[i] = '[=10=]';
// Start from next character
printf("%s ", &(temp[i]) + 1);
}
}
// printing the last word
printf("%s ", temp);
}
printf("\n");
return 0;
}
我做了这个程序来颠倒给定字符串中单词的顺序。 (而且有效)
即输出:sentence first the is This
但是,在向数组中添加另一个句子时,我遇到了困难。
例如,我需要一个数组 {"This is the first sentence", "And this is the second"}
作为输出生成:sentence first the is This , second the is this And
int main() {
char str[] = {"This is the first sentence"};
int length = strlen(str);
// Traverse string from end
int i;
for (i = length - 1; i >= 0; i--) {
if (str[i] == ' ') {
// putting the NULL character at the position of space characters for
next iteration.
str[i] = '[=10=]';
// Start from next character
printf("%s ", &(str[i]) + 1);
}
}
// printing the last word
printf("%s", str);
return 0;
}
我是 C 的新手,所以即使解决方案很简单,我还是卡住了也就不足为奇了。任何帮助,将不胜感激!谢谢!
因为您已经有了以相反顺序打印一个字符串的单词的代码,我建议制作一个 函数,它接受一个字符串作为参数,即:
void print_words_reverse(char * const str) {
// your current code here
}
然后你可以为每个字符串单独调用它:
char strings[][30] = {
"This is the first sentence",
"And this is the second"
};
for (int i = 0; i < sizeof(strings) / sizeof(*strings); ++i) {
print_words_reverse(strings[i]);
}
请注意,由于您正在修改字符串(通过用 NUL 字节替换空格),参数需要是可修改的,这意味着您不能(在标准 C 中)使用指向字符串文字的指针来调用它,这意味着您不能简单地使用 const char *strings[] = { "first", "second" }
。您可以通过让您的代码不修改参数字符串来摆脱为每个字符串保留的难看的常量长度(此处为 30
)。或者您可以为每个句子使用一个单独的字符数组,然后使用指向那些(可修改的)字符串的指针。
首先,您可以尝试使用二维数组或使用指针数组。
其次,在你的方法中,你丢失了字符串的初始值,我不知道它有多重要。
这是我使用指针数组的快速方法。
#include <stdio.h>
#include <string.h>
static void print_word(const char *str)
{
for (int i = 0; str[i] && str[i] != ' '; i++)
printf("%c", str[i]);
putchar(' ');
}
int main(void)
{
int len;
const char *str[] = {"This is the first sentence",
"And this is second", NULL};
for (int i = 0; str[i]; i++) {
for (len = strlen(str[i]); len >= 0; len--) {
if (len == 0)
print_word(&str[i][len]);
else if (str[i][len] == ' ')
print_word(&str[i][len + 1]);
}
putchar('\n');
}
printf("Initial value of array of strings [%s | %s] \n", str[0], str[1]);
return 0;
}
输出为:
sentence first the is This
second is this And
Initial value of array of strings [This is the first sentence | And this is second]
我建议您使用 memcpy
,但无需过多更改您的代码,这似乎可行
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX_STRING_LENGTH 100
int main()
{
char *str[] = {"This is the first", "And this is the second sentence"};
const size_t NUM_STRING = sizeof(str)/sizeof(char*);
/*%z used to print size_t variables*/
printf("%zd strings found\n", NUM_STRING);
int length[2];
int i;
for (i=0; i<NUM_STRING; i++)
{
length[i] = strlen(str[i]);
}
printf("length initialized %d %d\n", length[0], length[1]);
// Traverse string from end
int j = 0;
char temp[MAX_STRING_LENGTH];
printf("\n\n");
for (j=0; j<NUM_STRING; j++)
{
/*Make sure the string respect the MAX_STRING_LENGTH limit*/
if (strlen(str[j])>MAX_STRING_LENGTH)
{
printf("ERROR: string %d exceding max string length %d defined in constant "
"MAX_STRING_LENGTH. Exiting from program.\n", j, MAX_STRING_LENGTH);
exit(1);
}
//reset temporary string
memset(temp, '[=10=]', sizeof(temp));
//printf("temp variable reinitialized\n");
for (i = length[j] - 1; i >= 0; i--)
{
temp[i] = str[j][i];
if (str[j][i] == ' ')
{
// putting the NULL character at the position of space characters for next iteration.
temp[i] = '[=10=]';
// Start from next character
printf("%s ", &(temp[i]) + 1);
}
}
// printing the last word
printf("%s ", temp);
}
printf("\n");
return 0;
}