C 将字符串拆分为字符串数组
我试图将用户输入的一个句子拆分为一个单词数组,以便以后可以将单词作为字符串单独处理。 代码正在编译,但仅在用户输入后打印垃圾。 我尝试过调试,但没有发现问题。有人能帮我修一下吗C 将字符串拆分为字符串数组,c,arrays,string,C,Arrays,String,我试图将用户输入的一个句子拆分为一个单词数组,以便以后可以将单词作为字符串单独处理。 代码正在编译,但仅在用户输入后打印垃圾。 我尝试过调试,但没有发现问题。有人能帮我修一下吗 #include <stdio.h> #include <string.h> int main() { char str[1000]; int i = 0; char rev[1000][1000]; int r = 0; puts("Ent
#include <stdio.h>
#include <string.h>
int main() {
char str[1000];
int i = 0;
char rev[1000][1000];
int r = 0;
puts("Enter text:");
gets(str);
int k, length = 0;
printf_s("So the words are:\n");
while (str[i] != '\0') {
if (str[i] == ' ') {
k = i - length;
do {
rev[r][k] = (str[k]);
k++;
} while (str[k] != ' ');
printf(" ");
length = (-1);
r++;
} else
if (str[i + 1] == '\0') {
k = i - length;
do {
rev[r][k] = (str[k]);
k++;
} while (str[k] != '\0');
length = 0;
r++;
}
length++;
i++;
}
for (int r = 0; r < 1000; r++)
printf("%s ", rev[r]);
return 0;
}
#包括
#包括
int main(){
char-str[1000];
int i=0;
字符版本[1000][1000];
int r=0;
放入(“输入文本:”);
获取(str);
int k,长度=0;
printf_s(“因此单词是:\n”);
while(str[i]!='\0'){
如果(str[i]=''){
k=i-长度;
做{
rev[r][k]=(str[k]);
k++;
}while(str[k]!=“”);
printf(“”);
长度=(-1);
r++;
}否则
如果(str[i+1]='\0'){
k=i-长度;
做{
rev[r][k]=(str[k]);
k++;
}while(str[k]!='\0');
长度=0;
r++;
}
长度++;
i++;
}
对于(int r=0;r<1000;r++)
printf(“%s”,版次[r]);
返回0;
}
< /代码> 如果希望将字符串拆分为一个字符串数组,则应考虑<代码> Strtok < /Cuff>函数> <代码> >包含< /代码>。strtok
函数将分割给定分隔符上的字符串。对于您的情况,它将是“
使用教程中的strtok
示例:
#include <string.h>
#include <stdio.h>
int main(){
char str[80] = "This is - www.tutorialspoint.com - website";//The string you wish to split
const char s[] = "-";//The thing you want it to split from. But there is no need to this.
char *token;//Storing the string
/* get the first token */
token = strtok(str, s);//Split str one time using the delimiter s
/* walk through other tokens */
while( token != NULL )
{
printf( " %s\n", token );//Print the string
token = strtok(NULL, s);//Split the string again using the delimiter
}
return(0);
}
#包括
#包括
int main(){
char str[80]=“这是-www.tutorialspoint.com-website”;//要拆分的字符串
const char s[]=“-”;//要从中拆分的对象。但不需要这样做。
char*token;//存储字符串
/*获取第一个令牌*/
token=strtok(str,s);//使用分隔符s拆分str一次
/*浏览其他代币*/
while(令牌!=NULL)
{
printf(“%s\n”,标记);//打印字符串
token=strtok(NULL,s);//使用分隔符再次拆分字符串
}
返回(0);
}
替换您的声明
char rev[1000][1000];
与
之后你所有的代码
puts( "Enter text:" );
为此:
fgets( str, 998, stdin ); // Safe way; don't use gets(str)
const char delim[] = ",; "; // Possible delimiters - comma, semicolon, space
char *word;
/* Get the first word */
word = strtok( str, delim );
rev[i++] = word;
/* Get the next words */
while( word != NULL )
{
word = strtok( NULL, delim );
rev[i++] = word;
}
/* Testing */
for (int r = 0; r < i - 1; r++)
printf( "%s\n", rev[r] );
return 0
}
fgets(str,998,stdin);//安全通道;不使用gets(str)
常量字符delim[]=“,;”;//可能的分隔符-逗号、分号、空格
字符*字;
/*第一个字*/
word=strtok(str,delim);
rev[i++]=word;
/*了解下一个单词*/
while(word!=NULL)
{
word=strtok(NULL,delim);
rev[i++]=word;
}
/*测试*/
对于(int r=0;r
正如您所看到的,所有肮脏的工作都是通过strtok()
函数(“字符串到标记”)完成的,该函数遍历其他和其他单词(“标记”),识别它们是由字符串delim
中的一个或多个字符分隔的
像这样修复
#include <stdio.h>
int main(void) {
char str[1000];
char rev[1000][1000];
puts("Enter text:");
fgets(str, sizeof str, stdin);//Use fgets instead of gets. It has already been abolished.
int r = 0;
int k = 0;
for(int i = 0; str[i] != '\0'; ++i){
if (str[i] == ' ' || str[i] == '\n'){//is delimiter
if(k != 0){
rev[r++][k] = '\0';//add null-terminator and increment rows
k = 0;//reset store position
}
} else {
rev[r][k++] = str[i];
}
}
if(k != 0)//Lastly there was no delimiter
rev[r++][k] = '\0';
puts("So the words are:");
for (int i = 0; i < r; i++){
printf("%s", rev[i]);
if(i < r - 2)
printf(", ");
else if(i == r - 2)
printf(" and ");
}
return 0;
}
#包括
内部主(空){
char-str[1000];
字符版本[1000][1000];
放入(“输入文本:”);
fgets(str,sizeof str,stdin);//使用fgets而不是get。它已被废除。
int r=0;
int k=0;
对于(int i=0;str[i]!='\0';++i){
if(str[i]=''| | str[i]='\n'){//是分隔符
如果(k!=0){
rev[r++][k]='\0';//添加空终止符和增量行
k=0;//重置存储位置
}
}否则{
rev[r][k++]=str[i];
}
}
if(k!=0)//最后没有定界符
rev[r++][k]='\0';
puts(“所以单词是:”);
对于(int i=0;i
#包括
#包括
#包括
int count_空格(char*str)
{
如果(str==NULL | | strlen(str)。请改用。为什么不能使用strtok()
?rev[r][k]=(str[k]);
:rev[r][k]
的k
必须以0
开头,并且rev r][/code>必须以NUL字符结尾。感谢大家的帮助为什么998
中的大小是fgets()
当str是一个大小为1000的缓冲区时,fgets()
读取“…最多比大小小一个…”(至少根据man fgets
)?@SiggiSv-你可能是对的,我太懒了,所以为了安全起见,我使用了一些较小的值:-)从代码来看,该站点是教程点而不是教程点。最好包含指向该站点的链接以及找到代码的位置。它们的使用条款允许您在此处复制代码吗?
#include <stdio.h>
int main(void) {
char str[1000];
char rev[1000][1000];
puts("Enter text:");
fgets(str, sizeof str, stdin);//Use fgets instead of gets. It has already been abolished.
int r = 0;
int k = 0;
for(int i = 0; str[i] != '\0'; ++i){
if (str[i] == ' ' || str[i] == '\n'){//is delimiter
if(k != 0){
rev[r++][k] = '\0';//add null-terminator and increment rows
k = 0;//reset store position
}
} else {
rev[r][k++] = str[i];
}
}
if(k != 0)//Lastly there was no delimiter
rev[r++][k] = '\0';
puts("So the words are:");
for (int i = 0; i < r; i++){
printf("%s", rev[i]);
if(i < r - 2)
printf(", ");
else if(i == r - 2)
printf(" and ");
}
return 0;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int count_spaces(char *str)
{
if (str == NULL || strlen(str) <= 0)
return (0);
int i = 0, count = 0;
while (str[i])
{
if (str[i] == ' ')
count++;
i++;
}
return (count);
}
int count_char_from_pos(char *str, int pos)
{
if (str == NULL || strlen(str) <= 0)
return 0;
int i = pos, count = 0;
while (str[i] && str[i] != ' ')
{
count++;
i++;
}
return count;
}
char **get_words(char *str)
{
if (str == NULL || strlen(str) <= 0)
{
printf("Bad string inputed");
return NULL;
}
int i = 0, j = 0, k = 0;
char **dest;
if ((dest = malloc(sizeof(char*) * (count_spaces(str) + 1))) == NULL
|| (dest[0] = malloc(sizeof(char) * (count_char_from_pos(str, 0) + 1))) == NULL)
{
printf("Malloc failed\n");
return NULL;
}
while (str[i])
{
if (str[i] == ' ') {
dest[j++][k] = '\0';
if ((dest[j] = malloc(sizeof(char) * (count_char_from_pos(str, i) + 1))) == NULL)
{
printf("Malloc failed\n");
return NULL;
}
k = 0;
}
else {
dest[j][k++] = str[i];
}
i++;
}
dest[j][k] = 0;
dest[j + 1] = NULL;
return dest;
}
int main(void) {
char *line = NULL;
size_t n = 0;
getline(&line, &n, stdin);
printf("%s\n", line);
line[strlen(line) - 1] = 0;
printf("%s\n", line);
char **tab = get_words(line);
int i = 0;
while (tab[i])
{
printf("%s\n", tab[i++]);
}
}