[英]Printing a Sentence in C word by word, without using string.h
I'm currently learning how to program in C. As extra practice I was given some problems from my teacher. 我目前正在学习如何用C语言编程。作为额外练习,我的老师给了我一些问题。 Here is what my problem is, I have to use this in my program:
这是我的问题,我必须在我的程序中使用它:
void strSplitTokens (char *s1, char s2[][], int *numWords);
here is the rest of the problem: A call to strSplitTokens breaks string s1 into its words, it then saves each word in a distinct char array, and all the strings are saved in a single array of characters. 这是问题的其余部分: 对strSplitTokens的调用将字符串s1分解为单词,然后将每个单词保存在不同的char数组中,并且所有字符串都保存在单个字符数组中。 Also, it saves the number of words using pointers in numWords.
此外,它使用numWords中的指针节省了单词的数量。 print the strings in the main with the following:
使用以下内容打印主要字符串:
For(i=0;i<numWords;i++) printf("%s\n",s2[i]);
If I run it and give it a sentence like I like programming I need it to output: 如果我运行它并给它一个像我喜欢编程的句子我需要输出:
I 一世
Like 喜欢
Programming 程序设计
My main issue is that I was told not to use string.h, I am kind of lost on how to do it without it. 我的主要问题是我被告知不要使用string.h,如果没有它,我有点迷失方向。 Any advice and guidance would is greatly appreciated.
非常感谢任何建议和指导。
What I have so far is mostly mock up code which makes no sense: 到目前为止我所拥有的主要是模拟代码,这是没有意义的:
#include <stdio.h>
void strSplitTokens (char *s1, char s2[][], int *numWords);
int main()
{
printf("Enter a sentence: ");
strSplitTokens();
For(i=0;i<numWords;i++)
{
printf("%s\n",s2[i]);
}
return 0;
}
void strSplitTokens (char *s1, char s2[][], int *numWords);
{
char s1;
scanf("%s",&s1);
if( s1 != '\n')
{
strSplitTokens();
}
}
Check character by character. 逐个字符检查。 Put the each character in the two dimensional array until space comes.
将每个字符放在二维数组中,直到空间到来。
i=0;j=0;k=0;
while(s[k]!='\n') {
if(s[k]!=' ')
{
s1[i][j]=s[k];
k++;
j++;
}
else{
j=0;i++;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.