473,385 Members | 1,588 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

for loop skipping items

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[i]){
ligne[i] = alphabet[65-j];
printf("%d %c\n", i, fmt[i]);
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', '_', '-'};

Nov 14 '05 #1
7 1768
"Québec" <On**@WasEno.ugh> wrote in message news:7u********************@wagner.videotron.net.. .
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[i]){
ligne[i] = alphabet[65-j];
printf("%d %c\n", i, fmt[i]);
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[i]){
ligne[i] = alphabet[65-j];
printf("%d %c\n", i, fmt[i]);
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

Nov 14 '05 #2
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]; i++)
for (j = 0; j < alphas; j++) {
if (alphabet[j] == fmt[i]) {
ligne[i] = alphabet[alphas - j];
printf("%d %c\n", i, fmt[i]);
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[i]){
ligne[i] = alphabet[65-j];
printf("%d %c\n", i, fmt[i]);
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', '_', '-'};

Nov 14 '05 #3
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[i]- e -80- Ï
fmt[i]- e -80- Ï
fmt[i]- e -80- Ï
fmt[i]- 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[i]){
printf("fmt[i]- %c ", fmt[i]);
ligne[i] = alphabet[65-j];
printf("%d- %c\n",ligne[i]);
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);

}

Nov 14 '05 #4
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.

--
Er*********@sun.com

Nov 14 '05 #5
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
Nov 14 '05 #6
It works.

The beginner.
Nov 14 '05 #7
Eric Sosman <Er*********@sun.com> 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) ks***@mib.org <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.
Nov 14 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Chuck Anderson | last post by:
I have a function with 7 inputs. The last three have default values. I want to call that function specifying the first four, skip two and then specify the last. I thought I could write this...
13
by: TrintCSD | last post by:
How can I reset the collections within a foreach to be read as a change from within the foreach loop then restart the foreach after collections has been changed? foreach(string invoice in...
1
by: kaiser | last post by:
Hello. I am trying to write a program that continues to read the last line of a txt file until a stop button is clicked. while (ContinueLoop == true ) { while ((LastLine = file.ReadLine()) !=...
1
by: Tim | last post by:
Hi, I'm very new to .NET and am programming in C#. I have a web application where i have two list boxes. Its kind of like a shopping card where you can add items from one 'locations' list box to...
6
by: Ritesh Raj Sarraf | last post by:
Hi, Following is the code: def walk_tree_copy(sRepository, sFile, sSourceDir, bFound = None): try: if sRepository is not None: for name in os.listdir(sRepository): path =...
3
by: Egbert Nierop \(MVP for IIS\) | last post by:
Hi, Out of curiousity, I sometimes look at the produced assembly after compilation in release mode. What you often see, is that CPP, always fully addresses registers to copy values from a to...
3
by: vozzek | last post by:
Hi all, I'm a CSS rookie, but I'm stumped. Hopefully this is something simple that I'm overlooking. My master/detail page generates the following html code, and I've got it formatted with CSS...
6
by: Nobody | last post by:
hi there, suppose I want to iterate for a specific variable e.g. i , but for non regular (or consecutive) values. For example i=0,1,2,4,5,7,8 etc how can I do that with a for loop? MY...
16
by: Andy B | last post by:
I have the following code inside of a WebBrowser.DocumentCompleted event: For index As Integer = 0 To Me.Browser.Document.GetElementsByTagName("ul").Item(0).GetElementsByTagName("li").Count ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.