Thread: Pig Latin Code Help Please

  1. #1
    Registered User
    Join Date
    Apr 2017
    Posts
    17

    Pig Latin Code Help Please

    This is the code, I don't know why it won't work. Warnings are: comparison with string literal results in unspecified behavior (not sure what that means)

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    
    
    int main() {
    
    
    char word[30];
    char pig[30] = "";
    int len;
    char two[] = "";
    char one[] = "";
    int i;
    
    
    printf("Input the word you want to put into pig latin and backwards (MAX: 30 characters): \n");
    scanf(" %s ", word);
    len = strlen(word);
    
    
    for(i = 0; i < len; i ++) {
        word[i] = toupper(word[i]);
    }
    strncpy(two, word, 2);
    two[2] = '\0';
    
    
    strncat(one, word, 1);
    
    
    if (strcmp(two, "WH") == 0 || strcmp(two, "CH") == 0) {
        strcpy(pig, &word[2]);
        strcat(pig, two);
        strcat(pig, "AY");
    }
    else if (strcmp(two, "SH") == 0 || strcmp(two, "TH") == 0){
        strcpy(pig, &word[2]);
        strcat(pig, two);
        strcat(pig, "AY");
    }
    else if (strcmp(two, "PH") == 0 || strcmp(two, "QU") == 0) {
        strcpy(pig, &word[2]);
        strcat(pig, two);
        strcat(pig, "AY");
    }
    else if (&word[0] == "A" || &word[0] == "I") {
        strcpy(pig, word);
        strcat(pig, "AY");
    }
    else if (&word[0] == "E" || &word[0] == "O") {
        strcpy(pig, word);
        strcat(pig, "AY");
    }
    else if (&word[0] == "U") {
        strcpy(pig, word);
        strcat(pig, "AY");
    }
    else {
        strcpy(pig, &word[1]);
        strcat(pig, one);
        strcat(pig, "AY");
    }
    
    
    
    
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Apr 2017
    Posts
    80
    It's because your single characters, which are supposed to be char's (single characters) are in double quotes (double quotes mean strings, so you're using a single character string rather than a single character by using double quotes) – they should be char's so in single quotes. But then also you don't want the ampersands in front of your word[0] == 'E' expressions.
    Last edited by BpB; 04-11-2017 at 02:47 PM.

  3. #3
    Registered User
    Join Date
    Apr 2017
    Posts
    17
    ok, i changed it to this, and it still does not work. Warnings are: multi-character character constant and passing argument 2 of 'stromp' makes pointer from integer without a cast and expected 'const char *' but argument is of type 'int' - repeated for all if statements

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    
    
    int main() {
    
    
    char word[30];
    char pig[30] = "";
    int len;
    char two[] = "";
    char one[] = "";
    int i;
    
    
    printf("Input the word you want to put into pig latin and backwards (MAX: 30 characters): \n");
    scanf(" %s ", word);
    len = strlen(word);
    
    
    for(i = 0; i < len; i ++) {
        word[i] = toupper(word[i]);
    }
    strncpy(two, word, 2);
    two[2] = '\0';
    
    
    strncat(one, word, 1);
    
    
    if (strcmp(two, 'WH') == 0 || strcmp(two, 'CH') == 0) {
        strcpy(pig, &word[2]);
        strcat(pig, two);
        strcat(pig, 'AY');
    }
    else if (strcmp(two, 'SH') == 0 || strcmp(two, 'TH') == 0){
        strcpy(pig, &word[2]);
        strcat(pig, two);
        strcat(pig, 'AY');
    }
    else if (strcmp(two, 'PH') == 0 || strcmp(two, 'QU') == 0) {
        strcpy(pig, &word[2]);
        strcat(pig, two);
        strcat(pig, 'AY');
    }
    else if (word[0] == 'A' || word[0] == 'I') {
        strcpy(pig, word);
        strcat(pig, 'AY');
    }
    else if (word[0] == 'E' || word[0] == 'O') {
        strcpy(pig, word);
        strcat(pig, 'AY');
    }
    else if (word[0] == 'U') {
        strcpy(pig, word);
        strcat(pig, 'AY');
    }
    else {
        strcpy(pig, &word[1]);
        strcat(pig, one);
        strcat(pig, 'AY');
    }
    
    
    
    
    return 0;
    }

  4. #4
    Registered User
    Join Date
    Apr 2017
    Posts
    80
    What I was talking about only applied to your single character comparisons, word[0] == 'x' like expressions. You've changed those correctly, but then you've broken the previously correct string comparisons. Eg 'WH' isn't OK. Single quotes are for single characters, the char type. Put those back to how they were - make them strings again - and all should be well.

  5. #5
    Registered User
    Join Date
    Apr 2017
    Posts
    17
    OK, i did what you said, and also realised that I hadn't given the code to actually print pig. So I did a print statement and it says there are too many arguments for format. I'll post the code again, the print statement is at the bottom.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    
    
    int main() {
    
    
    char word[30];
    char pig[30] = "";
    int len;
    char two[] = "";
    char one[] = "";
    int i;
    
    
    printf("Input the word you want to put into pig latin and backwards (MAX: 30 characters): \n");
    scanf(" %s ", word);
    len = strlen(word);
    
    
    for(i = 0; i < len; i ++) {
        word[i] = toupper(word[i]);
    }
    strncpy(two, word, 2);
    two[2] = '\0';
    
    
    strncat(one, word, 1);
    
    
    if (strcmp(two, "WH") == 0 || strcmp(two, "CH") == 0) {
        strcpy(pig, &word[2]);
        strcat(pig, two);
        strcat(pig, "AY");
    }
    else if (strcmp(two, "SH") == 0 || strcmp(two, "TH") == 0){
        strcpy(pig, &word[2]);
        strcat(pig, two);
        strcat(pig, "AY");
    }
    else if (strcmp(two, "PH") == 0 || strcmp(two, "QU") == 0) {
        strcpy(pig, &word[2]);
        strcat(pig, two);
        strcat(pig, "AY");
    }
    else if (word[0] == 'A' || word[0] == 'I') {
        strcpy(pig, word);
        strcat(pig, "AY");
    }
    else if (word[0] == 'E' || word[0] == 'O') {
        strcpy(pig, word);
        strcat(pig, "AY");
    }
    else if (word[0] == 'U') {
        strcpy(pig, word);
        strcat(pig, "AY");
    }
    else {
        strcpy(pig, &word[1]);
        strcat(pig, one);
        strcat(pig, "AY");
    }
    printf("This is the pig latin: \n", pig);
    
    
    return 0;
    }

  6. #6
    Registered User
    Join Date
    Apr 2017
    Posts
    17
    I'm still new to C, and the help is very much appreciated

  7. #7
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    The printf needs a %s somewhere in the format string to tell it where to print the string.

    The scanf format shouldn't have the space before and after the %s. The one before it is redundant since it tells scanf to skip spaces but %s skips spaces anyway. The one after it will cause it to hang for more input.

    You aren't giving enough space for one and two. When you leave the brackets empty, the size is determined by the initializer. But you're initializing it with an empty string, which only contains the zero-terminator. So one and two are only 1 char long.

    And if you want to accept a max of 30 characters then word will need at least 31 chars in order to have space for the zero-terminator. And pig will need to be even longer since you are adding even more chars to it.

    Also, you're doing exactly the same thing in the first three if/else if's. And the same thing in the second group of three. So I don't see why they are separate.

  8. #8
    Registered User
    Join Date
    Apr 2017
    Posts
    17
    That worked! Thanks so much! Also, they are separated because my teacher told me it is good practice to only have a few comparisons in one if statement, rather than a whole bunch, because it can get confused by the computer or something :?
    Anyway, thank you!

  9. #9
    Registered User
    Join Date
    Apr 2017
    Posts
    17
    ok, the assignment was also supposed to be able to put the original word backwards, and so i tried (at the bottom) to do so, and it only prints the first letter. Why? and how to fix it?

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    
    
    int main() {
    
    
    char word[31];
    char pig[40] = "";
    int len;
    char two[3] = "";
    char one[2] = "";
    int i;
    
    
    printf("Input the word you want to put into pig latin and backwards (MAX: 30 characters) \n");
    scanf("%s", word);
    len = strlen(word);
    
    
    for(i = 0; i < len; i ++) {
        word[i] = toupper(word[i]);
    }
    strncpy(two, word, 2);
    two[2] = '\0';
    
    
    strncat(one, word, 1);
    
    
    if (strcmp(two, "WH") == 0 || strcmp(two, "CH") == 0) {
        strcpy(pig, &word[2]);
        strcat(pig, two);
        strcat(pig, "AY");
    }
    else if (strcmp(two, "SH") == 0 || strcmp(two, "TH") == 0){
        strcpy(pig, &word[2]);
        strcat(pig, two);
        strcat(pig, "AY");
    }
    else if (strcmp(two, "PH") == 0 || strcmp(two, "QU") == 0) {
        strcpy(pig, &word[2]);
        strcat(pig, two);
        strcat(pig, "AY");
    }
    else if (word[0] == 'A' || word[0] == 'I') {
        strcpy(pig, word);
        strcat(pig, "AY");
    }
    else if (word[0] == 'E' || word[0] == 'O') {
        strcpy(pig, word);
        strcat(pig, "AY");
    }
    else if (word[0] == 'U') {
        strcpy(pig, word);
        strcat(pig, "AY");
    }
    else {
        strcpy(pig, &word[1]);
        strcat(pig, one);
        strcat(pig, "AY");
    }
    printf("This is the pig latin: %s \n", pig);
    
    
    char backword[31];
    int n = 0;
    
    
    while(n < len) {
        strcpy(backword, &word[n]);
        n += 1;
    }
    
    
    printf("This is the word backwards: %s \n", backword);
    
    
    return 0;
    }

  10. #10
    Registered User
    Join Date
    Apr 2017
    Posts
    17
    P.S I also tried with a for loop (the same sort of instructions as well) and it did not work either

  11. #11
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    Don't use strcpy to copy single chars. Instead, something like backword[len - n - 1] = word[n].

    n++ is more usual than n += 1

    Don't forget to set backword[len] = '\0'.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Latin Square Generator. Code won't keep looping.
    By st00ch in forum C Programming
    Replies: 1
    Last Post: 09-28-2011, 09:47 AM
  2. Latin square
    By Ron in forum C Programming
    Replies: 6
    Last Post: 05-22-2006, 06:24 PM
  3. pig latin help
    By kashifk in forum C Programming
    Replies: 3
    Last Post: 04-13-2003, 08:46 AM
  4. pig latin help
    By noob2c in forum C Programming
    Replies: 4
    Last Post: 04-01-2003, 10:10 PM
  5. Pig Latin
    By hermit in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 11-20-2002, 08:35 AM

Tags for this Thread