如何将数组存储在地址C
How to store array in address C
基本上我们提供了主要功能:
#include <stdio.h>
#include <string.h>
#include <strings.h>
#define STORAGE 255
{
int c;
char s[STORAGE];
for(;;) {
(void) printf("n=%d, s=[%s]\n", c = getword(s), s);
if (c == -1)break;
}}
我们不会改变这一点
我们必须创建函数 getword(),它应该包含一个读取字符的循环,
将其存储在数组中的地址中,一个一个地存储它应该停止白色space (tab, space, eof)
基本上我有这个:
int getword(char *w)
{
char str[255];
int i = 0;
int charCount = 0;
printf("enter your sentence:\n");
gets(str);
/*this was provided by the professor, but i'm not sure how to use it
while (( iochar - getchar()) !=EOF);
*/
for(i = 0; str[i] != '[=11=]'; i++) //loop that checks tab and spaces
{
if(str[i] != ' ' && str[i] != '\t')
{
charCount++;
}
}
printf("your string: '%s' contains %d of letters\n", str, charCount);
return 0;}
现在程序的输出是:
enter your sentence:
hey stockoverflow
your string: 'hey stockoverflow' contains 16 of letters
n=0, s=[]
所以我正在保存字符串并计算它,但我没有将它存储在我应该存储的位置。
它应该显示 n=16,s=[嘿 stockoverflow]
其实应该显示n=3,s=[hey]
任何帮助将不胜感激
这可能是您要找的:
for(i = 0; str[i] != '[=10=]'; i++) //loop that checks tab and spaces
{
if(str[i] != ' ' && str[i] != '\t')
{
charCount++;
}
else
{
str[i] = '[=10=]'; // Terminate str
break; // Break out of the for-loop
}
}
printf("your string: '%s' contains %d of letters\n", str, charCount);
strcpy(w, str); // Add this line to copy str into w (which is s from main)
return strlen(w); // Return the length of the result string
基本上我们提供了主要功能:
#include <stdio.h>
#include <string.h>
#include <strings.h>
#define STORAGE 255
{
int c;
char s[STORAGE];
for(;;) {
(void) printf("n=%d, s=[%s]\n", c = getword(s), s);
if (c == -1)break;
}}
我们不会改变这一点
我们必须创建函数 getword(),它应该包含一个读取字符的循环,
将其存储在数组中的地址中,一个一个地存储它应该停止白色space (tab, space, eof)
基本上我有这个:
int getword(char *w)
{
char str[255];
int i = 0;
int charCount = 0;
printf("enter your sentence:\n");
gets(str);
/*this was provided by the professor, but i'm not sure how to use it
while (( iochar - getchar()) !=EOF);
*/
for(i = 0; str[i] != '[=11=]'; i++) //loop that checks tab and spaces
{
if(str[i] != ' ' && str[i] != '\t')
{
charCount++;
}
}
printf("your string: '%s' contains %d of letters\n", str, charCount);
return 0;}
现在程序的输出是:
enter your sentence:
hey stockoverflow
your string: 'hey stockoverflow' contains 16 of letters
n=0, s=[]
所以我正在保存字符串并计算它,但我没有将它存储在我应该存储的位置。
它应该显示 n=16,s=[嘿 stockoverflow]
其实应该显示n=3,s=[hey]
任何帮助将不胜感激
这可能是您要找的:
for(i = 0; str[i] != '[=10=]'; i++) //loop that checks tab and spaces
{
if(str[i] != ' ' && str[i] != '\t')
{
charCount++;
}
else
{
str[i] = '[=10=]'; // Terminate str
break; // Break out of the for-loop
}
}
printf("your string: '%s' contains %d of letters\n", str, charCount);
strcpy(w, str); // Add this line to copy str into w (which is s from main)
return strlen(w); // Return the length of the result string