在使用C字符串添加空间的每一句话结束 [英] add space to every word's end in a string in C
问题描述
在这里,我有一个字符串:
Here I have a string:
*line = "123 567 890 ";
,并在最后2位。我想这2位添加到3的结束和7的结束,使这样的:
with 2 spaces at the end. I wish to add those 2 spaces to 3's end and 7's end to make it like this:
"123 567 890"
我是想实现步骤如下:
I was trying to achieve the following steps:
- 解析字符串成单词列表字(字符串数组)。从上游的功能,我会得到变量的值WORD_COUNT,*线并保持。
- ,并在后面的空间将它们连接起来。
- 添加空间分布的方式,用左到右的优先级。
- 串联一切融合在一起,使之成为新的*线。
WORD_COUNT是在*行字数,仍然是空格数目依然存在。
Word_count is number of words in a *line, remain is the number of spaces remained.
下面是部分我的故障的code:
Here is a part of my faulty code:
int add_space(char *line, int remain, int word_count)
{
if (remain == 0.0)
return 0; // Don't need to operate.
int ret;
char arr[word_count][line_width];
memset(arr, 0, word_count * line_width * sizeof(char));
char *blank = calloc(line_width, sizeof(char));
if (blank == NULL)
{
fprintf(stderr, "calloc for arr error!\n");
return -1;
}
for (int i = 0; i < word_count; i++)
{
ret = sscanf(line, "%s", arr[i]); // gdb shows somehow it won't read in.
if (ret != 1)
{
fprintf(stderr, "Error occured!\n");
return -1;
}
arr[i] = strcat(arr[i], " "); // won't compile.
}
size_t spaces = remain / (word_count * 1.0);
memset(blank, ' ', spaces + 1);
for (int i = 0; i < word_count - 1; i++)
{
arr[0] = strcat(arr[i], blank); // won't compile.
}
memset(blank, ' ', spaces);
arr[word_count-1] = strcat(arr[word_count-1], blank);
for (int i = 1; i < word_count; i++)
{
arr[0] = strcat(arr[0], arr[i]);
}
free(blank);
return 0;
}
它不工作,你可以帮我看看,不工作,解决这些问题的部分吗?谢谢你们。
It is not working, could you help me find the parts that do not work and fix them please? Thank you guys.
推荐答案
的 的完成code加入到谷底,与修正原来的片段
complete code added to bottom, with corrections to original snippets
您的愿望的话,我的理解是需要一定长度的现有生产线,并重新写入相同的长度与原始的一个新行,但填写的均匀分布尽可能空格,没有空格后最后一个字:例如:
Your desire then, as I understand it is to take an existing line of some length, and rewrite it to a new line of the same length as the original, but filling in spaces as evenly distributed as possible, with no space after the last word: For example:
这是我的原句(年底四个空格 - 字符串长度为32)
"This is my original sentence " (four spaces at end - string length is 32)
| T | H |我| S | |我| S | | M | Y | |Ø| R | I | G | I | N |一个| L | | S | E | N | T | E | N | B | E | | | | |
|T|h|i|s| |i|s| |m|y| |o|r|i|g|i|n|a|l| |s|e|n|t|e|n|c|e| | | | |
您想重新分配空间,例如,原来的长度,32,为preserved:
You would like to redistribute the spaces such that the original length, 32, is preserved:
| T | H |我| S | | |我| S | | | M | Y | | |Ø| R | I | G | I | N |一个| L | | | S | E | N | T | E | N | B | E |
|T|h|i|s| | |i|s| | |m|y| | |o|r|i|g|i|n|a|l| | |s|e|n|t|e|n|c|e|.
以下应提供一组您可以在code实现跨线保持其原有的长度均匀地分配你的话步骤。
The following should provide a set of steps you can implement in your code to distribute your words evenly across a line maintaining its original length.
首先,店内原有的线长:
First, store original line length:
int origLen = strlen(line);
使用的strtok()
空间,,
作为分隔符,用于解析字符串第一次收集以下的目的:
Parse the string a first time using strtok()
with space, " "
as the delimiter, for the purpose of collecting the following:
1)的单词数的结果
2)的所有单词的累计长度的
1) the number of words
2) the cumulative length of all the words:
char *buf;
char temp[80];
char lineKeep[80];
int accumLen=0;
int wordCount=0;
int numSpaces;
strcpy(lineKeep, line);
buf = strtok(lineKeep, " ");
while(buf)
{
strcpy(temp, buf);
accumLen += strlen(temp)+1;//+1 because each word includes one following space
wordCount++;
buf = strtok(NULL, " ");
}
accumLen--; //remove last space
现在您可以重复解析,但是这个时候你的信息,你需要重新构建字符串,因为它被解析:
Now you can repeat the parse, but this time you have the information you need to re-construct the string as it is parsed:
numSpaces = (origLen - accumLen)+1;//determine number of trailing spaces after last word
//+1 to compensate for space at end of last word.
//parse line, place words and extra spaces
int spcToAdd; //number spaces added to every word except last
int extraSpc; //remainder spaces to distribute
spcToAdd = numSpaces/(wordCount - 1);
extraSpc = numSpaces%(wordCount - 1);
memset(lineKeep, 0, 80);
buf = strtok(line, " ");
while(buf)
{
strcat(lineKeep, buf);
for(i=0;i<spcToAdd;i++) strcat(lineKeep, " ");
if(extraSpc > 0) strcat(lineKeep, " "), extraSpc--;
buf = strtok(NULL, " ");
}
这应该这样做。
的结果
完整code,以更正原来的片段:的
#include <ansi_c.h>
int main(void)
{
char line[]="this is my original line ";
char *buf;
char temp[80];
char lineKeep[80];
int accumLen=0;
int wordCount=0, count;
int numSpaces;
int i;
int origLen = strlen(line);
strcpy(lineKeep, line);
printf("Original with \"*\" to demark spaces :*%s*\n", lineKeep);
buf = strtok(lineKeep, " ");
while(buf)
{
strcpy(temp, buf);
accumLen += strlen(temp)+1;//+1 because each word includes one following space
wordCount++;
buf = strtok(NULL, " ");
}
accumLen--; //remove last space
//second part
numSpaces = (origLen - accumLen);//determine number of trailing spaces after last word
//+1 to compensate for space at end of last word.
//parse line, place words and extra spaces
int spcToAdd; //number spaces added to every word except last
int extraSpc; //remainder spaces to distribute
spcToAdd = numSpaces/(wordCount - 1); //Add one extra space
extraSpc = numSpaces%(wordCount - 1); //while they last add additional space
memset(lineKeep, 0, 80);
count = 0;
buf = strtok(line, " ");
while(buf)
{
count++;
strcat(lineKeep, buf);
if(count < wordCount)
{
strcat(lineKeep, " "); //normally occuring space
for(i=0;i<spcToAdd;i++) strcat(lineKeep, " ");
if(extraSpc > 0) strcat(lineKeep, " "), extraSpc--;
}
buf = strtok(NULL, " ");
}
lineKeep[strlen(lineKeep)]=0;
printf("modified with \"*\" to demark spaces :*%s*\n", lineKeep);
getchar();
return 0;
}
此code结果的图像:的
Image of results from this code:
的 OP原来的code。与意见,并进行一些编辑的(不完全调试)
int add_space(char *line, int remain, int word_count)
{
int line_width; //added
if (remain == 0.0)
return 0; // Don't need to operate.
line_width = strlen(line);
int ret;
char arr[word_count][line_width]; //line width not originally defined,
memset(arr, 0, word_count * line_width * sizeof(char));
char *blank = calloc(line_width, sizeof(char));
if (blank == NULL)
{
fprintf(stderr, "calloc for arr error!\n");
return -1;
}
for (int i = 0; i < word_count; i++)
{
ret = sscanf(line, "%s", arr[i]); // gdb shows somehow it won't read in.
if (ret != 1) // each time this loops around, "this" is placed into arr[i];
{ // "line" is not changing, strtok will traverse line
fprintf(stderr, "Error occured!\n");
return -1;
}
//arr[i] = strcat(arr[i], " "); // won't compile.
strcpy(arr[i],strcat(arr[i], " ")); // assignment of char array to char array uses strcpy, or sprintf, et. al.
//each loop now adds " " -> "this "
// Note: you can assign char to char using =, but not char arrays
}
size_t spaces = remain / (word_count * 1.0); //size_t == uint, mult by float is set back into uint.
memset(blank, ' ', spaces + 1);
for (int i = 0; i < word_count - 1; i++)
{
//arr[0] = strcat(arr[i], blank); // won't compile.
strcpy(arr[i],strcat(arr[i], blank)); // Same as above. and index of arr[] should be i
}
memset(blank, ' ', spaces);
//arr[word_count-1] = strcat(arr[word_count-1], blank); //same
strcpy(arr[word_count-1],strcat(arr[word_count-1], blank)); //at this point, each arr[i]
//contains "test \0" (3 spaces and NULL)
for (int i = 1; i < word_count; i++)
{
//arr[0] = strcat(arr[0], arr[i]); //same
strcpy(arr[0], strcat(arr[0], arr[i]));
} //at this point arr[0] contains "test test test test t"
//ran out of room, note there is no NULL terminator '\0' at end.
free(blank);
return 0;
}
这篇关于在使用C字符串添加空间的每一句话结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!