C 键入一个函数getWord(),该函数接受字符串并返回指向单词开头的指针
我需要键入一个函数getWord(),该函数以字符串和整数作为参数。函数应该返回一个指针,指向它所引用的单词的开头。如果word为0,则返回指向字符串中第一个单词的指针;如果word为1,则返回指向第二个单词的指针,以此类推。如果word大于或等于字符串中的字数,则必须返回NULL 运行示例:C 键入一个函数getWord(),该函数接受字符串并返回指向单词开头的指针,c,string,pointers,C,String,Pointers,我需要键入一个函数getWord(),该函数以字符串和整数作为参数。函数应该返回一个指针,指向它所引用的单词的开头。如果word为0,则返回指向字符串中第一个单词的指针;如果word为1,则返回指向第二个单词的指针,以此类推。如果word大于或等于字符串中的字数,则必须返回NULL 运行示例: ptr = getWord ("hello you", 1); / * "you" returns * / ptr2 = getWord ("a string", 0); / * "a" return
ptr = getWord ("hello you", 1); / * "you" returns * /
ptr2 = getWord ("a string", 0); / * "a" returns * /
ptr3 = getWord ("one word", 2); / * NULL returns * /
void getWord(char string[])
{
int i;
for (i = 0; i < strlen(string); i++)
{
if (isalpha(string[i]))
printf("%c", string[i]);
if (string[i] == ' ')
break;
}
}
ptr=getWord(“你好”,1);/*“您”返回*/
ptr2=getWord(“字符串”,0);/*“a”返回*/
ptr3=getWord(“一个单词”,2);/*空返回*/
void getWord(字符字符串[])
{
int i;
对于(i=0;i
这就是你能做的
char* get_word (char *string,int req_num)
{
int i;
int ns;//number of spaces
int *sp;
ns=0;
for(i=0;string[i]!='\0';i++)
{
//check for a spaces and increment ns
if (string[i]==' ')
{
++ns;
sp=(int*)realloc(sp,ns*sizeof(int));
sp[ns-1]=(int)&string[i+1];
}
//check what place you have to return
if(req_num>=ns)
return NULL;
else
return (char*)sp[req_num];
}
}
这段代码未经优化,您可以想出一些方法来重新构造代码,使其优化以更快地运行。我们初学者应该互相帮助。:) 给你
#include <stdio.h>
#include <string.h>
char * getWord(const char *s, size_t n)
{
while ( ( s += strspn(s, " \t") ) != '\0' && n-- )
{
s += strcspn(s, " \t");
}
return *s ? (char *)s : (char *)0;
}
int main( void )
{
char *s = "Hi Adrian. How are you?";
size_t n = 0;
for (char *p; (p = getWord(s, n)) != NULL; ++n)
{
printf("%zu: %s\n", n, p);
}
printf("\nThere are %zu words in the string\n", n);
return 0;
}
如果你想得到这样的输出
0: Hi
1: Adrian.
2: How
3: are
4: you?
There are 5 words in the string
然后用以下方法重写主循环
for (char *p; (p = getWord(s, n)) != NULL; ++n)
{
size_t len = strcspn(p, " \t");
printf("%zu: %*.*s\n", n, ( int )len, ( int )len, p);
}
此getWord()返回输入字符串的第(n+1)个字:
char * getWord(char * string, int n)
{
char str[1024] = {'\0'};
char * delim = " \t";
char * pch = NULL;
char * result = NULL;
int i = 0;
if (string == NULL)
return NULL;
strcpy (str, string);
pch = strtok (str, delim);
while (pch != NULL)
{
if (i == n)
break;
pch = strtok (NULL, delim);
i++;
}
if (pch != NULL)
{
result = (char *)malloc(strlen(pch) + 1);
if (result == NULL)
printf ("Failed to allocate memory\n");
else
strcpy (result, pch);
}
return result;
}
如果此函数将结果返回为NOTNULL,请确保释放调用getWord()的函数中分配给“result”变量的内存
main()将如下所示-
int main ()
{
char * ptr;
ptr = getWord ("hello you", 1);
if (ptr != NULL)
{
printf ("ptr = %s\n", ptr);
free(ptr);
}
else
printf ("ptr is NULL");
return 0;
}
试试这个简单的主意。首先尝试实现如下功能:
char* next( const char* str )
{
const char delimiter = ' ';
// skip all characters up to the first ' '
while( *str != delimiter && *str != '\0' ) ++str;
// may the first characters is ' ', so skip it as well
while( *str == delimiter ) ++str;
return str;
}
好的,让我们测试一下:
int main( )
{
const char* first = next( "one two three four five" );
puts( first );
first = next( first );
puts( first );
first = next( first );
puts( first );
first = next( first );
puts( first );
}
其产出:
LSP ❱ ./a.out
two three four five
three four five
four five
five
LSP ❱
现在,我们需要告诉next
函数,该函数不仅指向下一个单词,而且还计算有多少个单词,因此变为:
char* next( const char* str, int index )
{
const char delimiter = ' ';
// take care of index for us
while( index-- ){
while( *str != delimiter && *str != '\0' ) ++str;
while( *str == delimiter ) ++str;
}
if( *str == '\0' ) return NULL;
return str;
}
让我们测试一下:
int main( )
{
puts( next( "one two three four five", 0 ) );
puts( next( "one two three four five", 1 ) );
puts( next( "one two three four five", 2 ) );
puts( next( "one two three four five", 3 ) );
puts( next( "one two three four five", 4 ) );
if( next( "one two three four five", 5 ) == NULL )
puts( "NULL was returned" );
}
以及:
LSP ❱ ./a.out
one two three four five
two three four five
three four five
four five
five
NULL was returned
LSP ❱
特色: 产出:
LSP ❱ ./a.out
four five
---------------
two three four five
three four five
four five
five
LSP ❱
要根据索引仅返回一个单词,可以使用
strdup
参见man3strdup
和strchr
,参见man3strchr
。因此,在下一个函数中
应该添加(while循环之后):
在主管道内部:
int main( )
{
char* r = next( "one two three four five", 3 );
if( r != NULL )
{
puts( r );
free( r );
}
else
{
puts( "there is no word at index 3" );
}
char* s = next( "one two three four five", 8 );
if( s != NULL )
{
puts( s );
free( s );
}
else
{
puts( "there is no word at index 8" );
}
}
测试:
注意对于
next2
只需添加:
if( *str == '\0' ) return NULL;
到第一个next
并将其命名为next2
您还应该检查
strdup
的返回值,它可能是NULL
您可能对该函数感兴趣?问题到底是什么?如果你要求我们为你编写一个函数,符合你的规范,那么,不。我们不这样做。我的函数,它的作用是打印出输入的第一个字。我需要使用指针选择要打印的单词的帮助。如果I
是单词第一个字母的索引,而不是前面的空格,则可以返回string+I
,因为编译器知道char
有多大,并且可以实现加法操作。但是,按照您的计算方法,您可能需要对i
的值进行一些计算。这个问题看起来有点困难,特别是如果行可以包含可变数量的空格,或者其他类型的空格。我认为即使你使用strtok(),你仍然需要做一些算术。你编写的程序所做的就是计算*s包含的单词数,这很酷!但是我想要的是用户写一个输入,然后从输入中选择在输出中写什么单词。例如:“输入句子:您好?”“您想输出什么单词:2”“是”@Adrian,您在问题中写道:“函数应返回指向其所指单词开头的指针”,显示的函数返回指向指定单词的指针。您可以使用此指针输出单词。这个程序只是演示了函数的正确工作。我几乎不理解函数,main看起来怎么样?@Adrain要理解函数getWord(),首先需要理解strtok()。我建议你仔细阅读一下strtok()。上面写着“put(next”(“一二三四五”,0));”的地方,应该只写上“one”,我怎么才能做到呢?@Adrian。如果您可以访问strchr
和strdup
,则很容易。“你有吗?”阿德里安。事实上,在下一个中复制str
,然后找到下一个空格,并使用strchr
,就是这样strchr
返回空白前的指针。例如:result[strchr(result')-result]='\0'
注意,您必须释放
重复的字符串
char* next( char* str, int index )
{
const char delimiter = ' ';
while( index-- ){
while( *str != delimiter && *str != '\0' ) ++str;
while( *str == delimiter ) ++str;
if( *str == '\0' ) return NULL;
}
// duplicate the current string
char* result = strdup( str );
// find the first whitespace and make it as NUL
result [strchr( result, ' ' ) - result ] = '\0';
if( *result == '\0' ) return NULL;
return result;
}
int main( )
{
char* r = next( "one two three four five", 3 );
if( r != NULL )
{
puts( r );
free( r );
}
else
{
puts( "there is no word at index 3" );
}
char* s = next( "one two three four five", 8 );
if( s != NULL )
{
puts( s );
free( s );
}
else
{
puts( "there is no word at index 8" );
}
}
LSP ❱ gcc -fsanitize=address temp.c
LSP ❱ ./a.out
four
there is no word at index 8
LSP ❱
if( *str == '\0' ) return NULL;