2016-09-25 47 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