-5

Here is a program with Strings where I am trying Pig Latin translation is simply taking the first letter of a “word” and appending that letter to the end of the word with “ay” added to the end as well

I have issue with m1=m2+3 ( resetting the Initial Marker ).

Input that I am giving : "Alex, how are you right"

The output I am expecting is : lexay, owhay reay ouyay ightray

But

I am getting this : lex,Aay way ay ayo 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;
}
Bhavya
  • 21
  • 4
  • 2
    You forgot to ask a question. If the question is how to debug a problem like this, you should tell us what debugger you are using. – David Schwartz Sep 25 '16 at 20:20
  • 1
    `fflush(stdin)` is undefined behavior and `gets` flirts with buffer overflow. – John Coleman Sep 25 '16 at 20:21
  • I can't figure out what you were hoping `piglatin[count+(n-1)]=english[count+1];` would do, but it can't possibly be right. The piglatin string will contain two extra characters per word, so how can `count+(n-1)` possibly be right? – David Schwartz Sep 25 '16 at 20:22
  • I don't mind having a look and find something obvious but here it's a good job for 1) a lot of print statements or 2) a debugger (with a lot of print commands) – Jean-François Fabre Sep 25 '16 at 20:23
  • 1
    This has an awful lot in common with your previous question, [Issues with string position appending C program](http://stackoverflow.com/questions/39690518/issue-with-string-position-appending-c-program), though it must be said that this code is better organized (not least because it is not using global variables). Note that [Using `fflush(stdin)`](http://stackoverflow.com/questions/2979209/using-fflushstdin) doesn't work sanely everywhere, and [`gets()` is too dangerous to be used — ever](http://stackoverflow.com/questions/1694036/why-is-the-gets-function-dangerous-why-should-it-not-be-used). – Jonathan Leffler Sep 25 '16 at 20:30
  • I need help to correct the program so that I can get the right output – Bhavya Sep 25 '16 at 20:32
  • @Jonathan Leffler : Could you please suggest any better solution for fflush(stdin) and gets(). my intention is get the string from user – Bhavya Sep 25 '16 at 20:33
  • It depends what you want to do. Often, `static inline void gobble(void) { int ch; while ((ch = getchar()) != EOF && ch != '\n') ; }` and calling `gobble()` is a way to deal with unread input from standard input. But you'd have seen code like that if you'd looked at the referenced question. Also, if you're working the Microsoft C runtime, `fflush(stdin)` is defined — but using it is not portable to the Unix world. Standard C says it is undefined. POSIX gives a limited behaviour (it doesn't do the same as Microsoft's version does). – Jonathan Leffler Sep 25 '16 at 20:44
  • For `gets()` alternatives, read the answers in the cross-referenced question. Again, it depends in part on which platform you're working on and what, if anything, you have in the way of portability requirements. – Jonathan Leffler Sep 25 '16 at 20:45
  • @Bhavya We really need to know what help you need. Do you have a debugger? Do you know how to use it? What specifically do you need help with? – David Schwartz Sep 25 '16 at 20:49
  • Sorry I don't have debugger – Bhavya Sep 25 '16 at 21:09

1 Answers1

0

I see various problems here:

  1. Alex -> lex,Aay: You should check for punctuation marks when determining the end of the words, thus inserting the Aay part before the comma character
  2. Alex -> lex,Aay: Every character from the start of a word should be converted to lowercase and the resulting first character should be converted to upper case respectively
  3. Now the conversion function: I have changed it a bit to get you started; it should work now ( at least it does with your test string ) without taking 1 and 2 into account though

    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);
        }
    }
    

I hope this gets you started in the right direction.

Please also check your array sizes for english and piglatin. The string for piglatin is alway longer than the english one but your array sizes are the same! Also i would advise you add some boundary checks to make sure you do not leave the array boundaries.

  • Thanks the answer, Could you please help me with boundary checks. Instead I am planning to assign memory using Memalloc, to avoid the array boundary issues but not expert with that – Bhavya Sep 25 '16 at 21:37
  • I guess you either mean std::malloc or CoTaskMemAlloc but in either case: You need to keep track of your dynamically allocated arrays sizes and check you are not indexing elements outside the range of 0 to size-1. If you can you should probably use some kind of string library, which will do all of the heavy lifting in memory management for you. – kaosinvictus Sep 25 '16 at 22:04