2016-02-15 60 views
0

我正在尝试编写一个程序,读取标准输入流查找单词(连续字母字符),并为每个单词将它左旋到第一个元音(例如“朋友”旋转到“iendfr” )并将该序列写出来代替原始单词。所有其他字符都将写入stdout中。在C元素周围旋转字母

到目前为止,我已经设法扭转了字母,但一直无法做更多。有什么建议么?

#include <stdio.h> 
#include <ctype.h> 
#include <string.h> 
#define MAX_STK_SIZE 256 

char stk[MAX_STK_SIZE]; 
int tos = 0; // next available place to put char 

void push(int c) { 
    if (tos >= MAX_STK_SIZE) return; 
    stk[tos++] = c; 
} 

void putStk() { 
    while (tos >= 0) { 
     putchar(stk[--tos]); 
    } 
} 

int main (int charc, char * argv[]) { 
    int c; 
    do { 
     c = getchar(); 
     if (isalpha(c) && (c == 'a' || c == 'A' || c == 'e' || c ==   'E' || c == 'i' || c == 'o' || c == 'O' || c == 'u' || c == 'U')) { 
      push(c); 
     } else if (isalpha(c)) { 
      push(c); 
     } else { 
      putStk(); 
      putchar(c); 
     } 
    } while (c != EOF); 
} 

-Soul

+0

''else else {putStk();}之前没有'c == EOF'的测试。 putchar(c);'需要审查。 – chux

+0

前两个if子句都做'push()' - 所以任何字符都被推,不是元音 - 是计划吗? – John3136

+0

您可以使用缓冲区而不是堆栈 – BLUEPIXY

回答

2

我不会写整个程序你,但这个例子说明如何旋转从第一个元音一个字(如果有的话)。函数strcspn返回与传递的集合中的任何匹配的第一个字符的索引,如果找不到匹配项,则返回字符串的长度。

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

void vowelword(const char *word) 
{ 
    size_t len = strlen(word); 
    size_t index = strcspn(word, "aeiou"); 
    size_t i; 
    for(i = 0; i < len; i++) { 
     printf("%c", word[(index + i) % len]); 
    } 
    printf("\n"); 
} 

int main(void) 
{ 
    vowelword("friend"); 
    vowelword("vwxyz"); 
    vowelword("aeiou"); 
    return 0; 
} 

程序输出:

iendfr 
vwxyz 
aeiou 
+0

另一种方法是'char * vowel = strpbrk(word,“aeiou”);''size_t index =元音 - 单词;'它基本上是'6对1','十几到另一个人。 –