2016-09-25 36 views
-5

这是一个带字符串的程序,我尝试 猪拉丁语翻译只是取一个“单词”的第一个字母,并将该单词的后面追加“ay”加到结束以及C中的字符串问题

我有问题与m1 = m2 + 3(重置初始标记)。

输入,我给: “亚历克斯,怎么是你的权利”

我期待的输出是:lexay,owhay雷伊ouyay ightray

我得到这个:法,AAY方式唉亚青gayi

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

void initialize(char english[], char piglatin[]); 
void readinput (char english[]); 
int countwords(char english[]); 
void convert (int words, char english[], char piglatin[]); 
void writeoutput(char piglatin[]); 


int main() 
{ 
    char english[80], piglatin[80]; 
    int words; 

    initialize(english, piglatin); 
    printf("enter the string\t"); 
    fflush(stdin); 
    gets(english); 
    printf ("\nInput buffer contents: %s\n", english); 
    words = countwords(english); 
    convert(words,english,piglatin); 
    writeoutput(piglatin); 
    printf ("Have a nice day\n"); 
} 

void initialize(char english[], char piglatin[]) 
{ 
    int count; 
    for(count =0; count<80;++count) 
    { 
     english[count]=piglatin[count]=' '; 

    } 
    return; 
} 

/* Scan the english test and determine the number of words */ 
int countwords(char english[]) 
{ 
    int count, words =1; 
    for (count =0;count <79;++count) 
    { 
     if(english[count]==' ' && english[count+1]!=' ') 
     ++words; 

    } 
    printf("%d\n",words); 
    return (words); 
} 

/* convert each words in to piglatin*/ 

void convert (int words, char english[], char piglatin[]) 
{ 
    int n, count; 
    int m1=0; 
    int m2; 

    /* convert each word */ 
    for (n=1;n<=words;++n) 
    { 
     /* locate the end of the current word*/ 
     count = m1; 
     printf ("\ before conversion word contents: %d\n", count); 
     while (english[count]!=' ') 
     { 
      m2=count++; 
     } 
     printf ("\ before conversion word contents: %d\n", m2); 
      /* transpose the first letter and add 'a', 'y'*/ 
     for (count =m1;count<m2;++count) 
     { 
      piglatin[count+(n-1)]=english[count+1]; 
     } 
     piglatin[m2+(n-1)] = english[m1]; 
     piglatin[m2+1] = 'a'; 
     piglatin[m2+2] = 'y'; 
     m1=m2+3; 
     printf ("\ Converted word contents: %s\n", piglatin); 

    } 
    return; 
} 

void writeoutput(char piglatin[]) 
{ 
    int count =0; 
    for (count =0; count <80; ++count) 
    { 
     putchar(piglatin[count]); 
    } 
    printf ("\n"); 
    return; 
} 
+2

你忘了问一个问题。如果问题是如何调试这样的问题,你应该告诉我们你正在使用的调试器。 –

+1

'fflush(stdin)'是未定义的行为,'gets'调用缓冲区溢出。 –

+0

我无法弄清楚你所希望的'piglatin [count +(n-1)] = english [count + 1];'会的,但它不可能是正确的。明胶字符串将包含每个单词两个额外的字符,那么'count +(n-1)'怎么可能是正确的呢? –

回答

0

我在这里看到各种各样的问题:

  1. Alex - > lex,Aay:在确定单词的结尾时应检查标点符号,从而在逗号字符前插入Aay部分
  2. Alex - > lex,Aay:从开头的每个字符字应该转换为小写字母,结果第一个字符应该分别转换为大写字母
  3. 现在转换函数:我改变了一下让你开始;现在应该工作(至少它与你的测试字符串一样)不考虑1和2的考虑,虽然

    void convert(int words, char english[], char piglatin[]) 
    { 
        int estart = 0; 
        int ppos = 0; 
        int m2; 
    
        for (int n = 0; n < words; n++) 
        { 
         //locate the start of the current word, to make 
         //sure something like this is converted: 
         //"Alex,  how are you" 
         while (english[estart] == ' ') 
         { 
          //make sure we do not exceed the strings boundaries! 
          if (english[estart] == '\0') 
          { 
           return; 
          } 
          estart++; 
         } 
         //locate the end of the word 
         int eend = estart; 
         while (english[eend] != ' ') 
         { 
          //never forget to check for the end of the string 
          if (english[eend] == '\0') 
          { 
           break; 
          } 
          eend++; 
         } 
         /* transpose the first letter and add 'a', 'y'*/ 
         for (int i = estart+1; i < eend; i++, ppos++) 
         { 
          piglatin[ppos] = english[i]; 
         } 
         piglatin[ppos++] = english[estart]; 
         piglatin[ppos++] = 'a'; 
         piglatin[ppos++] = 'y'; 
         //dont forget to add a whitespace or your string might behave   
         //very stangely! 
         piglatin[ppos++] = ' '; 
         estart = eend; 
    
         printf("\ Converted word contents: %s\n", piglatin); 
        } 
    } 
    

我希望这可以让你在正确的方向开始。

另请检查您的英文和明胶的阵列尺寸。明胶的字符串总是比英文字符串长,但是您的数组大小是相同的!此外,我会建议你添加一些边界检查,以确保你不离开数组边界。

+0

谢谢你的答案,请你帮我进行边界检查。相反,我正在计划使用Memalloc分配内存,以避免数组边界问题,但不是专家那 – Bhavya

+0

我想你要么意味着std :: malloc或CoTaskMemAlloc,但在任何情况下:您需要跟踪您的动态分配的数组大小和检查你是否索引0到1的范围之外的元素。如果你可以的话,你可能应该使用某种字符串库,它会为你做所有的内存管理工作。 – kaosinvictus