i have a string that is made out of a few sentences. for example:
我有一根由几个句子组成的绳子。例如:
hello world bye bye
now, i need to make this sentence into a coulmn of words:
现在,我需要把这句话变成一句话:
hello
world
bye
bye
i have this idea going on, but i dont know how to write it correctly, so i was hopiny ypu guys could help me out. this is what i have so far:
我有这个想法,但我不知道如何正确地写,所以我是hopiny ypu的人可以帮我。这是我目前所拥有的:
int len=0, k=0, stopatspace=0;
char temptext[100][15]={0};
char line[300]={0};
len=strlen(line);
printf("len is: %d", len);
for(k=0; k<len; k++)
{
if (k == ' ')
{
// i dont know what to write here in order to make it a cloumn
}
}
basiclly, my idea is to run on all the length of my line and when i reach a space i want it to enter (to go one line down so that it will look like a coulmn)
基本上,我的想法是在我的线的所有长度上运行当我到达一个空间时,我想要它进入(沿着一条直线向下,这样它看起来就像一个库仑)
4 个解决方案
#1
0
Suppose line
is the char
array that contains hello world bye bye
and text
is declared as
假设行是包含hello world bye的char数组,文本被声明为
char text[100][15]; //I used 100 and 15 because your example contains it
and you want each word to be copied into each row of text
. Then,use strtok()
function with " "
(space) as delimeter and place this in a loop that terminates when strtok()
returns NULL
to get each word. Copy each word to each row of text
using strcpy()
in the loop.
你希望每个单词被复制到每一行的文本中。然后,使用带有“”(空格)的strtok()函数作为delimeter,并将其放在一个循环中,在strtok()返回NULL以获取每个单词时终止循环。在循环中使用strcpy()将每个单词复制到每一行文本。
The code for this will look like this:
它的代码如下所示:
char text[100][15];
char line[]="hello world bye bye";
int i=0;
char *token=strtok(line," ");
while(token!=NULL)
{
strcpy(text[i],token);
i++;
token=strtok(NULL," ");
}
Now, to print it,you can use
现在,要打印它,你可以使用
for(int j=0;j<i;j++)
printf("text[%d]=%s",j,text[j]);
Another method would be to manually copy each character until a space is seen.
int len=strlen(line);
int i=0;
int k=0;
for(int j=0;j<len+1;j++)
{
if(line[j]==' ')
{
text[i][k]='\0';
i++;
k=0;
}
else
{
text[i][k]=line[j];
k++;
}
}
Note that the above code does not prevent buffer overflows. You can print each word using
注意,上面的代码并不能防止缓冲区溢出。你可以打印每个单词。
for(int j=0;j<i+1;j++)
printf("text[%d]=%s",j,text[j]);
#2
0
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
char string[100];
fgets(string, 100, stdin);
string[strlen(string)-1] = 0;
// create an array of pointers
char **string_array = (char**)malloc(sizeof(char*));
int i = 0, array_size;
// tokenize input
char *token = strtok(string, " ");
while(token!=NULL) {
// dynamically increase the array during run time
string_array = (char**)realloc(string_array, (i+1)*sizeof(char**));
// create the string as you would do when there is only one string
string_array[i] = (char*)malloc(strlen(token)+1);
strcpy(string_array[i], token);
token = strtok(NULL, " ");
i++;
}
array_size = i;
for(i=0; i<array_size; i++) {
printf("%s\n", string_array[i]);
}
return 0;
}
Basically you create an array of pointers and you allot memory for the strings one by one as you would do when there is only one string. (if number of token is unknown, use realloc to increase the size of pointer to pointers.)
基本上,你创建了一个指针数组,你为这些字符串分配了一个又一个的内存,就像你在只有一个字符串时所做的那样。(如果令牌数量未知,请使用realloc增加指向指针的指针的大小。)
#3
0
#include <stdio.h>
#include <ctype.h>
int main(void){
char line[300] = "hello world bye bye\n";
char temptext[100][15]={0};
int i=0, j, k=0;
while(line[k]){
if(isspace(line[k])){
++k;//skip space to word
continue;
}
for(j=0; j < 15-1 && line[k] && !isspace(line[k]); ++k, ++j)
temptext[i][j] = line[k];
if(j && ++i == 100)
break;
}
for(j=0; j<i; ++j)
puts(temptext[j]);
return 0;
}
#4
-1
#include<stdio.h>
#define NEWLINE printf("\n")
int main(void)
{
char string[]="hello world bye bye";
int index=0;
while(string[index])
{
if(string[index]==32)
{
NEWLINE;
index++;
}
else
{
printf("%c",string[index]);
index++;
}
}
NEWLINE;
}
// Whenever i encountered with a space, i am printing a new line on the screen. Here 32 is the ASCII value for space
#1
0
Suppose line
is the char
array that contains hello world bye bye
and text
is declared as
假设行是包含hello world bye的char数组,文本被声明为
char text[100][15]; //I used 100 and 15 because your example contains it
and you want each word to be copied into each row of text
. Then,use strtok()
function with " "
(space) as delimeter and place this in a loop that terminates when strtok()
returns NULL
to get each word. Copy each word to each row of text
using strcpy()
in the loop.
你希望每个单词被复制到每一行的文本中。然后,使用带有“”(空格)的strtok()函数作为delimeter,并将其放在一个循环中,在strtok()返回NULL以获取每个单词时终止循环。在循环中使用strcpy()将每个单词复制到每一行文本。
The code for this will look like this:
它的代码如下所示:
char text[100][15];
char line[]="hello world bye bye";
int i=0;
char *token=strtok(line," ");
while(token!=NULL)
{
strcpy(text[i],token);
i++;
token=strtok(NULL," ");
}
Now, to print it,you can use
现在,要打印它,你可以使用
for(int j=0;j<i;j++)
printf("text[%d]=%s",j,text[j]);
Another method would be to manually copy each character until a space is seen.
int len=strlen(line);
int i=0;
int k=0;
for(int j=0;j<len+1;j++)
{
if(line[j]==' ')
{
text[i][k]='\0';
i++;
k=0;
}
else
{
text[i][k]=line[j];
k++;
}
}
Note that the above code does not prevent buffer overflows. You can print each word using
注意,上面的代码并不能防止缓冲区溢出。你可以打印每个单词。
for(int j=0;j<i+1;j++)
printf("text[%d]=%s",j,text[j]);
#2
0
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
char string[100];
fgets(string, 100, stdin);
string[strlen(string)-1] = 0;
// create an array of pointers
char **string_array = (char**)malloc(sizeof(char*));
int i = 0, array_size;
// tokenize input
char *token = strtok(string, " ");
while(token!=NULL) {
// dynamically increase the array during run time
string_array = (char**)realloc(string_array, (i+1)*sizeof(char**));
// create the string as you would do when there is only one string
string_array[i] = (char*)malloc(strlen(token)+1);
strcpy(string_array[i], token);
token = strtok(NULL, " ");
i++;
}
array_size = i;
for(i=0; i<array_size; i++) {
printf("%s\n", string_array[i]);
}
return 0;
}
Basically you create an array of pointers and you allot memory for the strings one by one as you would do when there is only one string. (if number of token is unknown, use realloc to increase the size of pointer to pointers.)
基本上,你创建了一个指针数组,你为这些字符串分配了一个又一个的内存,就像你在只有一个字符串时所做的那样。(如果令牌数量未知,请使用realloc增加指向指针的指针的大小。)
#3
0
#include <stdio.h>
#include <ctype.h>
int main(void){
char line[300] = "hello world bye bye\n";
char temptext[100][15]={0};
int i=0, j, k=0;
while(line[k]){
if(isspace(line[k])){
++k;//skip space to word
continue;
}
for(j=0; j < 15-1 && line[k] && !isspace(line[k]); ++k, ++j)
temptext[i][j] = line[k];
if(j && ++i == 100)
break;
}
for(j=0; j<i; ++j)
puts(temptext[j]);
return 0;
}
#4
-1
#include<stdio.h>
#define NEWLINE printf("\n")
int main(void)
{
char string[]="hello world bye bye";
int index=0;
while(string[index])
{
if(string[index]==32)
{
NEWLINE;
index++;
}
else
{
printf("%c",string[index]);
index++;
}
}
NEWLINE;
}
// Whenever i encountered with a space, i am printing a new line on the screen. Here 32 is the ASCII value for space