Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 从字符串中打印一个单词_C - Fatal编程技术网

C 从字符串中打印一个单词

C 从字符串中打印一个单词,c,C,例如,我想打印某个字符串中的第三个单词 在下面的例子中——它是有效的,但是——可以用其他更好的方式来实现吗 #include <stdio.h> #include <string.h> char str1[20] = "This is string"; int a, b = 0, nums = 1; char sec[20]; int main() { for (a = 0; a <= strlen(str1); a++) { if (

例如,我想打印某个字符串中的第三个单词

在下面的例子中——它是有效的,但是——可以用其他更好的方式来实现吗

#include <stdio.h>
#include <string.h>

char str1[20] = "This is string";
int a, b = 0, nums = 1;
char sec[20];

int main()
{
    for (a = 0; a <= strlen(str1); a++) {

        if ( str1[a] != ' ')
        {
            printf ("%c", str1[a]);

           if (nums == 3)
            {
                sec[b] = str1[a];
                ++b;
            }

        } else {
            printf ("\n");
            ++nums;
        }
    }

    printf ("\n\n%s\n", str1);
    printf ("Total words count: %d\n", nums);

    printf("Third word: %s\n", sec);

    return 0;

}
#包括
#包括
char str1[20]=“这是字符串”;
int a,b=0,nums=1;
字符集[20];
int main()
{

对于(a=0;a也许这不是最优雅的解决方案,但你可以使用。假设你所有的单词都用空格分隔,那么这只是一个重复x次的问题。我只是很快地把这个东西组合起来,但我希望它能行得通

#include <string.h>
#include <stdio.h>

int main( void ) {
    char str[] = "This is a string\n";
    char *token;
    int i;
    token = strtok( str, " ");
    i = 1;
    while( token != NULL ) {
        if( 3 == i ) {
            fprintf( stdout, "%s\n", token );
            fflush( stdout );
        }
        token = strtok(NULL, " ");
        i++;  // <-- this was missing
    }
    return 0;
}
#包括
#包括
内部主(空){
char str[]=“这是一个字符串\n”;
字符*令牌;
int i;
标记=strtok(str,“”);
i=1;
while(令牌!=NULL){
如果(3==i){
fprintf(标准输出,“%s\n”,令牌);
fflush(stdout);
}
令牌=strtok(空,“”);

i++;//也许这不是最优雅的解决方案,但你可以使用。假设你所有的单词都用空格分隔,那么这只是一个重复x次的问题。我只是很快地把这个东西组合起来,但我希望它能正常工作

#include <string.h>
#include <stdio.h>

int main( void ) {
    char str[] = "This is a string\n";
    char *token;
    int i;
    token = strtok( str, " ");
    i = 1;
    while( token != NULL ) {
        if( 3 == i ) {
            fprintf( stdout, "%s\n", token );
            fflush( stdout );
        }
        token = strtok(NULL, " ");
        i++;  // <-- this was missing
    }
    return 0;
}
#包括
#包括
内部主(空){
char str[]=“这是一个字符串\n”;
字符*令牌;
int i;
标记=strtok(str,“”);
i=1;
while(令牌!=NULL){
如果(3==i){
fprintf(标准输出,“%s\n”,令牌);
fflush(stdout);
}
令牌=strtok(空,“”);
i++;//使用
%n
定位正在查找的子字符串的末端

不需要修改或复制源字符串

void Print_Nth_word(const char *s, unsigned n) {
  if (n > 0) {
    int left;
    int right;
    while (1) {
      left = right = 0;
      sscanf(s, " %n%*s%n", &left, &right);
      if (right <= left) return;  // no sub-string (word) found
      if (--n == 0) break;
      s += right;
    }
    printf("%.*s\n", right - left, &s[left]);
  }
}
void打印第n个字(常量字符*s,无符号n){
如果(n>0){
int左;
国际权利;
而(1){
左=右=0;
sscanf(s、%n%*s%n“、左侧和右侧);
如果(右使用
%n
定位正在查找的子字符串的末端

不需要修改或复制源字符串

void Print_Nth_word(const char *s, unsigned n) {
  if (n > 0) {
    int left;
    int right;
    while (1) {
      left = right = 0;
      sscanf(s, " %n%*s%n", &left, &right);
      if (right <= left) return;  // no sub-string (word) found
      if (--n == 0) break;
      s += right;
    }
    printf("%.*s\n", right - left, &s[left]);
  }
}
void打印第n个字(常量字符*s,无符号n){
如果(n>0){
int左;
国际权利;
而(1){
左=右=0;
sscanf(s、%n%*s%n“、左侧和右侧);

如果(右)使用strlen进行迭代是不好的。在“for”(a=0;a)中,可能更适合使用printf长度限制器:使用strlen进行迭代是不好的。在“for”中(a=0;a可能更适合使用printf长度限制器:请注意,strtok正在更改原始字符串。在调用strtok之前,您需要复制它。循环中缺少一个
i++
。已尝试编辑,但我的编辑正在等待同行审核。这是真的。仅使用strtok()当您不再使用原始字符串时,或将其复制到某个临时变量中,然后仅关闭该变量。请注意,strtok正在更改原始字符串。您需要在调用strtok之前复制它。循环中缺少一个
i++
。尝试编辑,但我的编辑正在等待同行审阅。这是正确的。仅使用strtok()当您不再使用原始字符串时,或者将其复制到某个临时变量,而只对其进行处理。