如何在c中的字符串中拆分单词?

时间:2016-08-26 13:09:02

标签: c

我需要构建一个程序,从用户接收最多30个字符,然后使用它。 例如,我需要翻转句子然后打印它,或者旋转它。 我一直试图将句子中的单词逐个复制到[30] [31]的矩阵中,但它不起作用......任何想法? 我不能用指针...... 感谢您的帮助:)

#include <stdio.h>
#include <string.h>
void main(){
    int i=0,
        j=0,
        wrongData=0,
        charCounter=0,
        word=0,
        letter=0;
    char st[100],
         arr[100]={0},
         mat[30][31]={0};

    printf("Please, enter your sentence >");    
    gets(st);

    while(i<strlen(st)){
        if('A'<=st[i] && st[i]<='Z'){
            charCounter++;
            arr[j] = st[i];
            i++;
            j++;
        } else if(st[i]==' '){
            arr[j] = ' ';
            i++;
            j++;
            while(st[i] == ' '){
                i++;
            }
        } else if(st[i]=='\0'){
            arr[j] = '\0';
            break;
        } else {
            puts("ERROR: Incorrect data, try again.");
            wrongData=1;
            break;
        }

        if(wrongData==0){
            if(charCounter>30){
                puts("ERROR: Incorrect data, try again.");
            }
        }
    }

    puts(st);
    puts(arr);
    if(arr[j]==' '){
        word++;
    }

    while(arr[j]!=' ' && letter<32){
        strcpy(mat[word],arr);
    }

    if(arr[j]=='\0'){
        mat[word][letter]=arr[j];
    }       

    puts(mat[word]);
}

3 个答案:

答案 0 :(得分:0)

考虑您的评论

  

问题是我需要扭转单词而不是字母... for   例如:如果字符串是猫讨厌狗,我需要到最后   狗讨厌猫

然后我认为你的意思是以下

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

char * reverse_words( char s[] )
{
    for ( char *p = s, *q = s; *p; p = q )
    {
        while ( isspace( ( unsigned char )*p ) ) ++p;

        q = p;

        while ( *q && !isspace( ( unsigned char )*q ) ) ++q;

        for ( size_t i = 0; i < ( q - p ) / 2; i++ )
        {
            char c = p[i];
            p[i] = q[-i-1];
            q[-i-1] = c;
        }
    }

    for ( size_t i = 0, n = strlen( s ); i < n / 2; i++ )
    {
        char c = s[i];
        s[i] = s[n-i-1];
        s[n-i-1] = c;
    }

    return s;
}

int main( void ) 
{
    char s[] = "cats hates dogs";

    puts( s );
    puts( reverse_words( s ) );

    return 0;
}

程序输出

cats hates dogs
dogs hates cats

答案 1 :(得分:0)

这是另一种方法。我们的想法是通过字符串并记录每个单词开始和结束的索引。然后可以以相反的顺序打印单词。 (顺便说一句 - 旋转单词也很容易。)

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

int main() {
  char st[100] = "here we go again";
  int start[30] = { 0 };
  int end[30] = { 0 };
  int count = 0;
  int len = strlen(st);
  int i, j;


  // Find start and end index of each word
  start[0] = 0;
  for(i = 0; i < len; ++i)
  {
    if (st[i] == ' ')
    {
      end[count] = i;
      ++count;
      start[count] = i + 1;
    }
  }
  end[count] = len;

  // Print the words in reverse order
  for(i=count; i >= 0; --i)
  {
    for (j = start[i]; j < end[i]; ++j)
    {
      printf("%c", st[j]);
    }
    printf(" ");
  }
  printf("\n");

  return 0;
}

输出:

  

再次转到我们这里

答案 2 :(得分:0)

修复你的方法:

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

#define MAX_LEN 30

int main(void){
    int i, j, n, word;
    char st[100], arr[100], mat[MAX_LEN / 2][MAX_LEN + 1];

    printf("Please, enter your sentence (up to %d chars and A-Z or space)\n>", MAX_LEN);fflush(stdout);
    scanf("%99[^\n]%*c", st);

    //validate and reduce of spaces
    for(j = i = 0; st[i]; ++i){
        if(i > MAX_LEN){
            fputs("ERROR: Incorrect data, try again.\n", stderr);
            return 1;
        }
        if('A'<=st[i] && st[i]<='Z'){
            arr[j++] = st[i];
        } else if(st[i]==' '){
            arr[j++] = ' ';
            while(st[++i] == ' ')//Skip a continuous space
                ;
            --i;//one back for next loop
        } else {
            fputs("ERROR: Incorrect data, try again.\n", stderr);
            return 1;
        }
    }
    arr[j] = '\0';//st[i]=='\0' never become true in loop

#if DEBUG
    puts(st);
    puts(arr);
#endif

    //split to word
    for(word = j = i = 0; arr[i];){
        while(arr[i] == ' ')
            ++i;//skip space
        while(arr[i] != ' ' && arr[i] != '\0')
            mat[word][j++] = arr[i++];
        mat[word++][j] = '\0';
        j = 0;
    }
#if DEBUG
    for(i = 0; i < word; ++i)
        puts(mat[i]);
#endif
    puts("reverse word");
    for(i = 0; i < word; ++i){
        if(i)
            putchar(' ');
        printf("%s", mat[word-1-i]);
    }
    puts("\nrotate word");
    printf("Please, enter number of rotate\n>");fflush(stdout);
    scanf("%d", &n);
    for(i = 0; i < word; ++i){
        if(i)
            putchar(' ');
        printf("%s", mat[(i+n)%word]);//rotate left
    }
}