我需要输入一个函数getWord(),它接受一个字符串和一个整数作为参数。该函数应返回指向它所引用的单词的开头的指针。如果word为0,则返回指向字符串中第一个字的指针,如果word为1,则返回指向第二个字的指针,等等。如果word大于或等于字符串中的字数,必须返回NULL。
示例运行:
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;
}
}
答案 0 :(得分:1)
我们初学者应该互相帮助。:)
你在这里。
#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 Adrian. How are you?
1: Adrian. How are you?
2: How are you?
3: are you?
4: you?
There are 5 words in the string
如果你想得到像这样的输出
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);
}
答案 1 :(得分:0)
这是你可以做的
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];
}
}
此代码未经优化,您可以采用重组代码的方法来优化代码以更快地运行代码。
答案 2 :(得分:0)
此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;
}
如果此函数将结果返回为非NULL,请确保释放分配给&#34;结果&#34;的内存。函数中的变量,调用getWord()。
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;
}
答案 3 :(得分:0)
尝试这个简单的想法
首先尝试实现这样的next
函数:
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 ❱
int main( )
{
// 1. skip white space
puts( next( "one two three four five", 3 ) );
// returns: 'four five'
puts( "---------------" );
// 2. easy iteration over a string
const char* first = "one two three four five";
while( ( first = next2( first ) ) != NULL )
{
puts( first );
}
}
输出:
LSP ❱ ./a.out
four five
---------------
two three four five
three four five
four five
five
LSP ❱
要仅根据索引返回单词,您可以strdup
使用man 3 strdup
和strchr
,请参阅man 3 strchr
。因此,在next function
内你应该添加(在while循环之后):
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 ❱
请注意 next2
只需添加:
if( *str == '\0' ) return NULL;
到第一个next
并将其命名为next2
另外,您应该检查strdup
的返回值NULL