for loop skipping items

Discussion in 'C Programming' started by Québec, Jul 30, 2004.

  1. Québec

    Québec Guest

    How comes the for loop just printf 3 characters?


    1 e
    7 e
    10 e

    The string mixed by C is : J?an Pi?rr?

    =========
    fmt string is "Jean Pierre"
    ---------------
    const char *fmt;
    .....
    strcpy(ligne, fmt);
    ....
    for(i=0;i<11;i++){
    for(j=0;j<65;j++){
    if(alphabet[j] == fmt){
    ligne = alphabet[65-j];
    printf("%d %c\n", i, fmt);
    break;
    }
    }
    }

    ===
    char alphabet[] = {
    '0','1','2','3','4','5','6','7','8','9',
    'A','B','C','D','E','F','G','H','I','J', 'K','L','M','N',
    'O','P','Q','R','S','T','U','V','W','X','Y','Z', ' ',
    'a','b','c','d','e','f','g','h','i','j','k','l','m','n',
    'o','p','q','r','s','t','u','v','w','x','y','z', '_', '-'};
     
    Québec, Jul 30, 2004
    #1
    1. Advertising

  2. Québec

    Tim Hagan Guest

    "Québec" <> wrote in message news:7usOc.67415$...
    > How comes the for loop just printf 3 characters?
    >
    >
    > 1 e
    > 7 e
    > 10 e
    >
    > The string mixed by C is : J?an Pi?rr?


    Where did the question marks come from? They are not in alphabet[].

    > =========
    > fmt string is "Jean Pierre"
    > ---------------
    > const char *fmt;
    > ....
    > strcpy(ligne, fmt);


    I don't understand the need for the strcpy.

    > ...
    > for(i=0;i<11;i++){
    > for(j=0;j<65;j++){
    > if(alphabet[j] == fmt){
    > ligne = alphabet[65-j];
    > printf("%d %c\n", i, fmt);
    > break;
    > }
    > }
    > }
    >
    > ===
    > char alphabet[] = {
    > '0','1','2','3','4','5','6','7','8','9',
    > 'A','B','C','D','E','F','G','H','I','J', 'K','L','M','N',
    > 'O','P','Q','R','S','T','U','V','W','X','Y','Z', ' ',
    > 'a','b','c','d','e','f','g','h','i','j','k','l','m','n',
    > 'o','p','q','r','s','t','u','v','w','x','y','z', '_', '-'};


    The for loops are fine. It must be something else that you are not
    showing us. By knocking this mess into compilable shape (and adding a
    printf statement at the end) we get this:

    #include <stdio.h>
    #include <string.h>

    int main(void)
    {
    char alphabet[] = {
    '0','1','2','3','4','5','6','7','8','9',
    'A','B','C','D','E','F','G','H','I','J', 'K','L','M','N',
    'O','P','Q','R','S','T','U','V','W','X','Y','Z', ' ',
    'a','b','c','d','e','f','g','h','i','j','k','l','m','n',
    'o','p','q','r','s','t','u','v','w','x','y','z', '_', '-'};
    const char *fmt = "Jean Pierre";
    char ligne[12];
    int i, j;

    strcpy(ligne, fmt);
    for(i=0;i<11;i++){
    for(j=0;j<65;j++){
    if(alphabet[j] == fmt){
    ligne = alphabet[65-j];
    printf("%d %c\n", i, fmt);
    break;
    }
    }
    }
    printf("%s\n", ligne);
    return 0;
    }

    which prints:

    0 J
    1 e
    2 a
    3 n
    4
    5 P
    6 i
    7 e
    8 r
    9 r
    10 e
    jOSFTdKOBBO

    It looks good to me.

    --
    Tim Hagan
     
    Tim Hagan, Jul 30, 2004
    #2
    1. Advertising

  3. Québec wrote:
    > How comes the for loop just printf 3 characters?


    Since what you posted [retained at EOM] isn't anywhere close to a
    compilable program, there's no way to tell. See if the following is
    close to what you want:

    #include <stdio.h>
    #include <string.h>

    int main(void)
    {
    char alphabet[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ "
    "abcdefghijklmnopqrstuvwxyz_-";
    const int alphas = sizeof alphabet - 1;
    int i, j;

    char fmt[] = "Jean Pierre";
    char ligne[BUFSIZ]; /* choose an appropriate size */
    strcpy(ligne, fmt);
    for (i = 0; fmt; i++)
    for (j = 0; j < alphas; j++) {
    if (alphabet[j] == fmt) {
    ligne = alphabet[alphas - j];
    printf("%d %c\n", i, fmt);
    break;
    }
    }
    }


    [Québec's post continued]
    >
    >
    > 1 e
    > 7 e
    > 10 e
    >
    > The string mixed by C is : J?an Pi?rr?
    >
    > =========
    > fmt string is "Jean Pierre"
    > ---------------
    > const char *fmt;
    > ....
    > strcpy(ligne, fmt);
    > ...
    > for(i=0;i<11;i++){
    > for(j=0;j<65;j++){
    > if(alphabet[j] == fmt){
    > ligne = alphabet[65-j];
    > printf("%d %c\n", i, fmt);
    > break;
    > }
    > }
    > }
    >
    > ===
    > char alphabet[] = {
    > '0','1','2','3','4','5','6','7','8','9',
    > 'A','B','C','D','E','F','G','H','I','J', 'K','L','M','N',
    > 'O','P','Q','R','S','T','U','V','W','X','Y','Z', ' ',
    > 'a','b','c','d','e','f','g','h','i','j','k','l','m','n',
    > 'o','p','q','r','s','t','u','v','w','x','y','z', '_', '-'};
    >
    >
    >
     
    Martin Ambuhl, Jul 30, 2004
    #3
  4. Québec

    Québec Guest

    Hi again sorry for the missing code, I thought it was too much. If you never
    did JNI stay away:) Anyway, now I am sure the for loops where good.

    Java calls C witch returns the modified string with the jstring ret.

    Here is the out put

    The string 'Jean Pierre' received by C has 11 characters.
    fmt- e -80- Ï
    fmt- e -80- Ï
    fmt- e -80- Ï
    fmt- 85- Ï
    The string mixed by C is : [?om8§
    in java: [?om8§

    Thanks

    Jean
    ===========
    #include "Serial.h"
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <stddef.h>
    #define JNI_FALSE 0
    #define JNI_TRUE 1

    char alphabet[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ " <from a
    friend on this group ...
    "abcdefghijklmnopqrstuvwxyz_-";
    const int alphas = sizeof alphabet - 1;


    JNIEXPORT jstring JNICALL Java_Serial_numero
    (JNIEnv *env, jobject jobjc, jstring jstr){
    jboolean iscopy;
    jstring ret;
    jsize len;
    int i, j;
    const char *fmt;
    char alphabet[65];
    char ligne[12];

    fmt = (*env)->GetStringUTFChars(env, jstr, &iscopy);
    len = (*env)->GetStringLength(env, jstr);

    printf("The string '%s' received by C has %d characters.\n", fmt, len );

    for(i=0;i<11;i++){
    for(j=0;j<alphas;j++){
    if(alphabet[j] == fmt){
    printf("fmt- %c ", fmt);
    ligne = alphabet[65-j];
    printf("%d- %c\n",ligne);
    break;
    }
    }
    }
    ligne[12] = '\0';
    printf("The string mixed by C is : %s\n", ligne);


    ret = (jstring)(*env)->NewStringUTF(env, ligne);
    (*env)->ReleaseStringUTFChars(env, jstr, fmt);

    return ret;
    }
    =================
    /* DO NOT EDIT THIS FILE - it is machine generated */
    #include <jni.h>
    /* Header for class Serial */

    #ifndef _Included_Serial
    #define _Included_Serial
    #ifdef __cplusplus
    extern "C" {
    #endif
    /*
    * Class: Serial
    * Method: numero
    * Signature: (Ljava/lang/String;)Ljava/lang/String;
    */
    JNIEXPORT jstring JNICALL Java_Serial_numero
    (JNIEnv *, jclass, jstring);

    #ifdef __cplusplus
    }
    #endif
    #endif
    ===========

    public class Serial
    { static
    {
    System.loadLibrary("serial");
    }

    public static void main(String[] args)
    {
    Serial serie = new Serial();
    String fromC = serie.numero("Jean Pierre");
    System.out.println("in java: " + fromC);
    }

    public native String numero(String one);

    }
     
    Québec, Jul 30, 2004
    #4
  5. Québec

    Eric Sosman Guest

    Québec wrote:
    > Hi again sorry for the missing code, I thought it was too much. If you never
    > did JNI stay away:) Anyway, now I am sure the for loops where good.


    I've never done JNI, but it scares me far less than
    the putrid code you've written. The immediate error (or
    *an* immediate error) is

    > char alphabet[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ " <from a
    > friend on this group ...
    > "abcdefghijklmnopqrstuvwxyz_-";
    > const int alphas = sizeof alphabet - 1;
    >
    > JNIEXPORT jstring JNICALL Java_Serial_numero
    > (JNIEnv *env, jobject jobjc, jstring jstr){
    > [...]
    > char alphabet[65];


    Now, when you mention to `alphabet[j]' in the code below,
    which of the *two* *completely* *different* *arrays* named
    `alphabet' do you suppose is referenced? The one you so
    carefully initialized outside the function, or the still
    uninitialized ("filled with random garbage") array inside
    the function? Go on, take a wild guess ...

    The fundamental problem, Québec, is that you do not
    understand the tool you are trying to use. You don't know
    any C, yet you're trying to program in C. Lacking knowledge,
    you throw together miscellaneous bits and pieces that look
    just a little bit like C, then you stir them randomly until
    the compiler stops complaining, and then you wonder why they
    behave in peculiar ways.

    Perhaps you think this is a good way to proceed -- after
    all, it's the way natural selection works, and the organisms
    evolution has produced are marvellous. Unfortunately, natural
    selection requires a lot of time. A *lot* of time, as in
    billions of years. I don't know about your schedule, but my
    own doesn't permit me to wait that long for a working program.

    Spend some time to learn C, Québec. You may feel that the
    time is wasted because you're not working on The Job, but it
    will be repaid. At the rate you're going, completing The Job
    is likely to require more time than it would take you to learn
    C, Java, APL, Common Lisp, and Attic Greek, all put together.

    --
     
    Eric Sosman, Jul 30, 2004
    #5
  6. Québec

    Québec Guest

    But I do know a bit of c and much more on Java.

    The obvious error you mention has probably and will hunt you for the rest of
    your days. For example, the next time you will miss a step climbing you
    stairs, in your home that you have climbed all your life.

    I am happy for the guy who took off my mask and shown me my real Me.

    Thank you anyway for paying attention.

    I have learn to appreciate every bit of that kind.

    Jean
     
    Québec, Jul 30, 2004
    #6
  7. Québec

    Québec Guest

    It works.

    The beginner.
     
    Québec, Jul 30, 2004
    #7
  8. Eric Sosman <> writes:
    [...]
    > The fundamental problem, Québec, is that you do not
    > understand the tool you are trying to use. You don't know
    > any C, yet you're trying to program in C. Lacking knowledge,
    > you throw together miscellaneous bits and pieces that look
    > just a little bit like C, then you stir them randomly until
    > the compiler stops complaining, and then you wonder why they
    > behave in peculiar ways.
    >
    > Perhaps you think this is a good way to proceed -- after
    > all, it's the way natural selection works, and the organisms
    > evolution has produced are marvellous. Unfortunately, natural
    > selection requires a lot of time. A *lot* of time, as in
    > billions of years. I don't know about your schedule, but my
    > own doesn't permit me to wait that long for a working program.


    To expand on that point a bit ...

    Different programming languages vary in the amount of checking that is
    done, or that can be done, before you can actually run the program.

    Suppose a programmer writes, say, several hundred lines of code before
    first trying to compile it. The compiler is likely to report a number
    of errors: typos, syntax errors, missing declarations, etc. The
    programmer then fixes the reported errors and tries compiling again,
    iterating until the compilation is successful.

    In some languages, once you've gotten the code to compile cleanly,
    there's a fairly decent chance that it will actually work. (<OT>In my
    experience, Ada is such a language; Cobol may be another, but I've
    never used it.</OT>) That's not to say that logical errors are
    impossible in such languages, but many kinds of errors are likely to
    be caught by the compiler. (I'm absolutely not trying to start a
    language war, just making an observation.)

    C is not such a language. There are a number of seemingly minor
    errors you can make in C that will break the program, or at least
    change its meaning. Consider:

    #include <stdio.h>

    int main(void)
    {
    int i;

    for (i = 0; i < 10; i ++)
    printf("i = %d\n", i);

    printf("========================================\n");

    for (i = 0; i < 10; i ++);
    printf("i = %d\n", i);

    printf("========================================\n");

    for (i = 0; i < 10; i ++)
    printf("i = %d", i);
    printf("\n");

    return 0;
    }

    It's easy for a novice programmer to assume that each of the three
    blocks of code starting with "for" is going to do the same thing, but
    the second and third behave very differently. (Do you see why?) Both
    gcc with all warnings enabled and Splint accept the above program
    without complaint. A code formatter is likely to show the problem
    (that's a hint).

    The C philosophy, to the extent that there is such a thing, is to
    assume that the programmer knows what he's doing. That makes it easy
    for an experienced programmer to write correct code, but it also makes
    it easy for any programer, experienced or not, to make mistakes.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Jul 31, 2004
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Vidal
    Replies:
    3
    Views:
    2,222
  2. branton ellerbee

    sqlDataReader skipping first row in loop

    branton ellerbee, Jun 2, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    6,342
    Teemu Keiski
    Jun 2, 2004
  3. Tad McClellan
    Replies:
    3
    Views:
    160
    Edward Wijaya
    May 13, 2004
  4. Isaac Won
    Replies:
    9
    Views:
    397
    Ulrich Eckhardt
    Mar 4, 2013
  5. ccc31807

    skipping blank array items

    ccc31807, Sep 6, 2013, in forum: Perl Misc
    Replies:
    14
    Views:
    245
    Willem
    Sep 8, 2013
Loading...

Share This Page