#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    1
    Rep Power
    0

    String Comparision


    Hello
    I Want some help regardng my program.
    I have to write a program that uses the following initialized string table to translate between french and english.
    char words[5][2][10]=
    {
    {"See You","Au Revoir"},
    {"Computer","Ordinateur"},
    {"I","Je"},
    {"Yes","Oui"},
    {"Hello","Bonjour"},
    };
    Now the program takes an english word as an input from user and compares to the item in the string table,if the words exits in the table the program prints its French translation of the word otherwise prints "The word does not exists".
    Here is my code but it is having problem in comparing the variable....
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    /* run this program using the console pauser or add your own getch, system("pause") or input loop */

    int main(int argc, char *argv[])
    { int f,i,j;
    char words[5][2][10]=
    {
    {"See You","Au Revoir"},
    {"Computer","Ordinateur"},
    {"I","Je"},
    {"Yes","Oui"},
    {"Hello","Bonjour"},
    };
    char a[10];
    printf("Enter Any Word\n");
    scanf("%s",&a[10]);


    for (i = 0; i < 5; i++)
    {
    f=strcmp(a,words[i][0]);

    }
    if(f==1)
    {
    printf("\n%s",words[i][1] );
    }}


    }
    plz help me
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,385
    Rep Power
    1871
    Please edit your post to include [code][/code] tags around the actual code, so we can read it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,158
    Rep Power
    2222
    Use code tags to preserve your code's indentation. Neglecting to do so turns your code into an unreadable mess.

    Here is what your code looks like with code tags -- original formatting recovered via the Reply button:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    /* run this program using the console pauser or add your own getch, system("pause") or input loop */
    
     int main(int argc, char *argv[])
    {	int f,i,j;
    	char words[5][2][10]=
    	{
    		{"See You","Au Revoir"},
    		{"Computer","Ordinateur"},
    		{"I","Je"},
    		{"Yes","Oui"},
    		{"Hello","Bonjour"},
    	};
    char a[10];
    printf("Enter Any Word\n");
    scanf("%s",&a[10]);
    		
    	
    for (i = 0; i < 5; i++) 
    { 
    f=strcmp(a,words[i][0]);
    
    }
    if(f==1)
    {
    printf("\n%s",words[i][1] );
    }}
    
     
       }
    Gee, that is really crapping formatting. You need to apply indenting consistently and always in accordance with the levels of indentation. Here is what it should look like:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    /* run this program using the console pauser or add your own getch, system("pause") or input loop */
    
    int main(int argc, char *argv[])
    {
        int f,i,j;
        char words[5][2][10]=
        {
            {"See You","Au Revoir"},
            {"Computer","Ordinateur"},
            {"I","Je"},
            {"Yes","Oui"},
            {"Hello","Bonjour"},
        };
        char a[10];
        
        printf("Enter Any Word\n");
        scanf("%s",&a[10]);
        
        for (i = 0; i < 5; i++) 
        { 
            f=strcmp(a,words[i][0]);
        }
        
        if(f==1)
        {   
            printf("\n%s",words[i][1] );
        }
    }
     
    }
    You have mismatched braces. You couldn't see that because of your truly crappy formatting, but it becomes apparent immediately when proper formatting is applied.

    Also, your code doesn't even compile. Why did you neglect to tell us that? For that matter, you didn't even try to describe the problem at all. We need information to be able to help you. If you withhold pertinent information, then we cannot help you. It's your choice.

    Here is what I get when I try to compile:
    C:TEST>gcc -Wall im2lrn_1.c
    im2lrn_1.c: In function `main':
    im2lrn_1.c:8: warning: unused variable `j'
    im2lrn_1.c:31: warning: control reaches end of non-void function
    im2lrn_1.c: At top level:
    im2lrn_1.c:33: parse error before `}'

    C:TEST>
    Lose that extra close brace.

    Also, you told the compiler that you would return an int, but then you broke your promise. You lied to the compiler. You must never lie to the compiler:
    Originally Posted by Henry Spencer
    If you lie to the compiler, it will get its revenge.
    When your program has run successfully, you return 0. When you have to abort because of some problem (eg, you need to open a file but that file open fails) then you return a positive non-zero value.

    Other than that, I am at a total loss as to what you think you are doing in your program. You are going through all the pairs of words and comparing them, but you save the result of all of them to the same variable, f. That means that when you complete that loop, f will contain the results of the very last comparison, meaning that all the other comparisons have been ignored and thrown away.

    And if the very last comparison failed to find a match (read the documentation for strcmp, especially the section on what the return values mean), then you try to print out a word that does not even exist.

    Read your code for comprehension! Read what you are telling the computer to do! That requires you to keep track of the values of all the variables. What are the valid values for i when applied to the words array? 0 through 4. Would 5 be a valid value for accessing a row in that array? No, it would not. When you complete the for-loop, what is the value of i? 5, which is not a valid value for accessing a row in words.

    When you find a match in words, wouldn't you want to stop looking and jump out of that loop? Or continue to plow through the for-loop but at least save the index that had given you a match? And wouldn't you want to print out the match only if you had a match? So why aren't you testing for the words being equal? RTFM! ("Read The Manual!")

    Also, for "Je", what happens when the user instead types in "je"? Do you still want it to match in that situation?
  6. #4
  7. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,110
    Rep Power
    1803
    "Ordinateur" is too long for your fixed length array. The string is 10 characters plus the nul terminator.

IMN logo majestic logo threadwatch logo seochat tools logo