Adak 419 Nearly a Posting Virtuoso

Hey,

Remove the ampersand & from the scanf() line of code. Strings don't work with that, just numbers and stuff.

Your char arrays are very small. Remember that all strings need a space for the end of string marker: '\0', which scanf() will try and place onto the end of the string.

You can't flush your kitchen faucet, only the toilet will flush. fflush(stdin), is undefined. Use fflush() with OUTWARD streams, only, not INWARD streams like stdin.

use getchar(); to remove the last newline from the key buffer stream. It IS necessary, sometimes.

Adak 419 Nearly a Posting Virtuoso

Can you draw arcs by modifying the algorithm? yes.

But you can't use the neat algorithmic trick of the circle drawing algorithm, to that same exent (all four quads at once).

Maybe define a midline and try using both sides of the distance from that midline.

I'm thinking of a midline which intersects the arc in the center, at 90°. Points on the arc (both sides), would share an equal distance from a point on the midline.

I haven't done this, it's just an idea.

Adak 419 Nearly a Posting Virtuoso

Describe your array, and which way you want to "flip" it - Rotate it 90° clockwise or counter-clockwise, or along a particular axis (specify which one). A 3 X 3 example of the array before and after the flip, would be great.

Use code tags around the example, or it will be mangled by the forum software.

Adak 419 Nearly a Posting Virtuoso

With two for loops, one nested inside the other. The outer for loop manages the rows, and the inner for loop manages the variables for the columns inside the row being printed.

Remember that the total width of the diagram, minus the number of stars you print on that row, is the number of spaces that must be printed on the row.

Adak 419 Nearly a Posting Virtuoso

You only need two for loops, one nested inside the other.

The outer for loops handles how many rows, and the inner for loop handles the printing on that row.

for(r = ....etc ) {
   for(c = ....etc ) {
      with every loop you'll either add or subtract one or two to the r
      variable, which is quite nice, because it relates closely with how many 
      stars to print up in that row. 
   }
}

I usually use two int variables, "r" and "c" or "i" and "j" for this. Laid out like the above pattern, it's not too tough. All that other code is ready for the dustbin. You can see how hard it is to code up a simple assignment, when you don't have a plan.

Since our pattern repeats but in an upside down way, you may want to do half of it, and then do the other half, in a second nested pair like the first, but with different logic.

Adak 419 Nearly a Posting Virtuoso

Why not change the sscanf() format to %lf for the doubles you want (leave the %s for the name alone of course), and then change char dat0[][] to double data0, and see!

Extremely high probability of success (as in I've already tested it to be sure, but I didn't want to give you the ENTIRE answer).

I'm feeling a lessening of sweat equity on this program, from the OP.

Adak 419 Nearly a Posting Virtuoso

Some food for thought:

/*
7
aluminum .012580 .003000 32. 1130.
cast-iron .005441 .001747 32. 1160.
ingot-iron .006375 .001636 32. 1380.
malleable-iron .006503 .001622 32. 930.
ingot-steel .006212 .001623 32. 1380.
copper .009278 .001244 32. 1160.
nickel .007652 .001023 32. 1830.
*/

#include <stdio.h>

int main(void) {
   FILE *fp;
   int i, rowNum;
   char buff[100];
   char name[50][30];   //use malloc or a linked list, to get just what you need, later on
   char dat0[50][30];   //use a struct to unite name and data, later on
   char dat1[50][30];   //so you have one array of structs.
   char dat2[50][30];
   char dat3[50][30];

   if((fp=fopen("metalData.txt", "r")) ==NULL) {
      printf("Error: metalData file did not open!\n");
      return 1;
   }
   
   fscanf(fp, "%d",&rowNum); printf("rows:%d\n", rowNum);
   fgetc(fp); //most necessary, removes the newline char fgets won't do that for you
   
   for(i=0;i<rowNum;i++) {
      printf("\n");
      //get and print the first line of data
      fgets(buff, sizeof(buff), fp); printf("%s", buff); //get one row in the file
      //pull the data into separate arrays, and print them
      sscanf(buff, "%s %s %s %s %s", name[i],dat0[i],dat1[i],dat2[i],dat3[i]); printf("%s %s %s %s %s\n",name[i],dat0[i],dat1[i],dat2[i],dat3[i]); getchar();
   }
   printf("\nEeach row of metal data should be printed twice, and have the same content\n");
   fclose(fp);    
   printf("\n");
   return 0;
}
Adak 419 Nearly a Posting Virtuoso

Wrong format. Use:

filePointer = fopen("filename", "w");

Your file mode letter can be "r", "w", "a" "rt", "wt","rb" or "wb". The one's with a b are for binary mode, t is for text (and your system may have a default set by a variable option for your system). You can also use + between the letters, etc.

You should check your filePointer to be sure it's not NULL after the open line of code. It's common to have a filename be just a letter off, etc. Filenames are case sensitive, btw.

Adak 419 Nearly a Posting Virtuoso

Control+Z (aka ^Z), is the end of file indicator, for DOS and Windows (all versions). It is an integer, and can't be detected as a char. So there's your while (or for) loop:

while(first integer != EOF) {
    //your code here
}

Whenever possible, use integers as your data type, not floats. Here, you have floating point variables for the number of cars!
I doubt very much if 0.22 of a car, will every use your parking lot! ;)

Be sure to change your scanf() and printf() format indicators, as well.
And look at that! You need an integer to test for EOF, and now you know you also need an integer for other data, as well. What a coincidence! ;)

Adak 419 Nearly a Posting Virtuoso

Thanx Adak. That helped. What really are these Environmental Variables? and purpose do these paths serve>

Environmental variables tell the OS where your OS is found, and other system info, that it needs, while it's booting up (and uses from then on). The PATH for instance, tells the part of the OS that is booting up, (which it finds from the master boot record), where to find the rest of the files - programs for the dll's to run, and all the so-called "external" commands of Windows. (External commands are commands that either use a lot of memory or are used less frequently. The OS doesn't keep these in memory, but loads them only when you need them.)

If the Turbo C directory you're running from is not in the path, any system() lines of code will crash the program it's in, or be ignored. All depending on the phase of the moon, and the lottery numbers that week. (in other words, it's undefined).


In WindowsXP, (and probably other versions as well), it's a bit confusing because it has two command shells: command and cmd. Command is the normal Windows command shell, while cmd is the console command shell, which can work with DOS sized programs of 16 bit. Each one of these has it's own separate configuration file ("config.sys" for cmd), and autoexec.bat (for cmd), file. In addition, the registry has PATH and other configuration info, stored in it. Windows 7 will not run the …

Adak 419 Nearly a Posting Virtuoso

DOS had a path environmental variable, in a file named "config.sys". Windows has something similar, sometimes it uses config.sys, and sometimes it uses another file, depending on the version. The registry also has data on this.

Best answer is to look up PATH in your Windows help, which will have the right info for your version of Windows. In some versions, you can change your PATH (careful now!), right in Control Panel, look for the System >> environmental variables tab, iirc. In Windows 7 it's:

Control Panel >> System >> Advanced System Settings >> Advanced Tab >> Environmental Variables button.

Adak 419 Nearly a Posting Virtuoso

Welcome to the forum, Wurdig! ;)

Remove the switch case. It just makes things more difficult.

You'll need two nested for loops. The outer one will range from 0 to < MaxRows. The inner for loop is for the columns in one row, and will range from 0 to < MaxCols.

One or two if statements inside the inner for loop, will give the logic needed to either print a char (putchar('*'), or print a blank char (putchar(' ').

It's very helpful to see the logical steps needed, if you do it several times by hand, with tree's of different sizes, with paper and pencil first. Keep your code as simple as possible. Simple is good!

Adak 419 Nearly a Posting Virtuoso

Thanx Adak. You are right about my sofware engineering ambition. I am running DOS under windows and I am totally cognisant of the fact that DOS is cadevorous rotting operating system, but don't you think that learning DOS before going for windows could give me a better understanding of how things work at the elmentary level, and how things really began manfesting.

Also let me know if you can provide me with some useful resources which you think might be of help, especially if you have things regarding Intel Processors and their architecture and instruction set.

I would skip DOS, then. Google up info on the 8088 or 8086 cpu (first PC cpu), and then work your way up through the 80186, 80286 and especially the 80386. The AMD (not Intel) design for keeping compatibility with 8088 software, while moving to a flat memory model, etc., was a HUGE breakthrough. Intel couldn't match it, and paid big bucks to use the AMD design (which is still the basis for the cpu, although it's been enhanced many times over by both Intel and AMD).

For hands on learning though, you can't beat the simple(r) micro boards like Arduino, and etc. Those little kits are a blast and a half, and (if you don't burn them up), then can be used for several different projects. There, the simplicity of the board makes so many things jell in how digital micro electronics work.

You can't run TSR's on top of Windows …

Adak 419 Nearly a Posting Virtuoso

I used to have a book with some examples of this - haven't seen the book in years, but I'll look around and see if I can find it.

The problem with TSR's was, they would work on PC's with a certain design, and peripherals, and OS, but anything else was a maybe, and the further you got away from the author's design of PC, the less of a chance there was of his TSR programs (or any assembly program), working.

TSR's worked if they were made for a particular PC and OS, etc., but they were never officially supported by Microsoft, unless they were made by MS.

From your other forum's posts, I believe you'd really like software engineering, and a small microprocessor board based project, would be right up your alley. Have you looked into any of the Arduino board or other micro board forums/projects?

The more you look into TSR's, the more you'll find they're sort of the smelly rotting corpse, end of the PC engineering world.

But I will look for the book. ;)

What version of DOS are you running? You're not running this inside a Virtual Box/Virtual Machine type environment, right?

Adak 419 Nearly a Posting Virtuoso

Windows puts constraints on what you can do in this regard, depending on the version of Windows you are running. You are very likely to crash either the TSR program, another program running at that time, or even Windows itself.

Modern way to do this, would be to use a service, perhaps. Check with your operating system.

Adak 419 Nearly a Posting Virtuoso

Because '\n' is not part of your *substring

I wouldn't use fseek(), unless it was the only way possible to make it work. Any time you can avoid making the disk head move around, I'd avoid it. On big files or lots of runs, it introduces relatively long delays (shorter delays with SSD's, but still a delay), and extra wear and tear on disk heads, if you have the data on HD's.

I'd go with a "flag" (a boolean type variable), that you set when the match == strlen(substring). Then, when it loops and a char has been stored in ch, add an if statement, if ch is a space, or a punctuation, then the match is confirmed, so handle it then, and of course, reset your variables, including the flag.

Adak 419 Nearly a Posting Virtuoso

Welcome to the forum, Bubbinos! ;)

Change your logic. Start with the array of numbers, being assigned to the array:

for(i=0;i<52;i++) {  //eliminate repeated numbers
  array[i] = i;
}
//now pick a random card
for(i=0;i<(NumPlayers * 5); i++) {
  j = rand() % 52 + 1; //range of 1 to 52
  hand[i] = array[j]; //a unique random number
}

I haven't run the above, but I believe it's OK. Notice how nice and easy it is to study it, when it's between tags.

You should ALWAYS use code tags - just click on the [code ] icon in the editor top banner area. That puts the code tags on the page for you, and places your cursor right between them. Just paste your program there, and you're done.[code ] tags.

You should ALWAYS use code tags - just click on the icon in the editor top banner area. That puts the code tags on the page for you, and places your cursor right between them. Just paste your program there, and you're done.[code ] icon in the editor top banner area. That puts the code tags on the page for you, and places your cursor right between them. Just paste your program there, and you're done.

Adak 419 Nearly a Posting Virtuoso

He gives the example of a line of text, not having the entire word, like draw
ing.

Using fgets() and strstr(), for "drawing", you wouldn't find the word if it was split like this.

I agree that's a simple and sweet algorithm Walt, but it can't handle split words without "hurdles". ;)

Adak 419 Nearly a Posting Virtuoso

I don't mean to complain, but after 41 posts manicaction, you should be putting your code between code tags on the forum, every time. Very few of us are going to spend time looking over code that looks like html.

Use fgets(), or don't, it's your choice. There's more than one way to do this, clearly.

Did you try Narue's code? Did you try Manicaction's code?

I just thought of a simple way that might appeal to you. Say the word being searched for is "draw".

open the file, and set int match = 0.

Until the end of the file is reached. In a loop, look for a 'd' char. When you find
one, match++, and check the next char. If it's an 'r', then match++, and continue looping. Break out of the loop when you reach a space, or a punctuation (use ispunct() ), or exceed the strlen() of the target word.

If you reach a newline, and the match variable equals the strlen() of the target word, then you've found an instance of the word, in the text. If the match variable is less than the strlen() of the target word, then go to the next line of text, and continue seeing if the char's match the next letter in the target word.

The first letter in the text that doesn't match the target word (except the above case), causes the program to break out of the loop.

Adak 419 Nearly a Posting Virtuoso

Very true, Narue.

The OP said he was searching in a "text file", which I took to mean standard lines and having complete words on each line.

If he has hyphenated words, or words that are arbitrarily split into a two lines of text, that would require more effort.

Adak 419 Nearly a Posting Virtuoso

I believe Narue has graciously answered your questions/concerns. Your point is well taken, that a literal searcher won't give you everything you need, without more coding work.

It seems that to get just the "ask" (whole word), you need logic to handle " ask", except in the case where the "a" in "ask", is the first letter in the line of text. Then it needs to match "ask ", "ask, ", or "ask. ".

Note that:
draw
ing

will certainly require extra code, because it simply isn't a word, until it's re-assembled by your code.

Imo, when using fgets(), make it 1/3rd larger than your biggest line of text, unless you know just what the correct size should be. Memory isn't that precious, and truncating data is usually the last thing you want to do.

Adak 419 Nearly a Posting Virtuoso

Ff the target word is found, you can do the rest of your code to handle the target word being found, and then simply break out of the searching loop.

If you want to find the target word 5 times, just break out when the counter == 5.

Seems quite straight forward.

Adak 419 Nearly a Posting Virtuoso

Welcome to the forum, Jamblaster! ;)

Always click on the [code ] icon and when it puts two code tags on your reply page, paste your code, between the code tags - easy to do since it puts your cursor right between them for you.

All comparison sorts except for 1 obscure one, require two loops - one nested inside the other. You are sorting one pass OK, but that is just one pass, not everything.

This is the pseudo code for a simple substitution sort (like a bubble sort, but a bit quicker/easier, and no bubbles. (not always quicker, but usually).

int temp

for(i...;; i++) {
  for(j=i+1;...; j++) {
     if(array[i] > array[j]) {
        do your swap of array[i] and array[j], and use a temp variable
     }
  }
}
Adak 419 Nearly a Posting Virtuoso

No, this something you can easily do yourself, and just involves a bit of problem solving. You NEED to develop your problem solving, or you can't program worth a darn.

Work through it by hand, and see what kind of loops you're using!

Put some sweat into it! ;) Don't look at the recursive code - don't look at any code, right now. Learn how YOU would solve for LCD, using a loop (which is very natural, btw)

Adak 419 Nearly a Posting Virtuoso

Use fgets() to take in at least one line of text, at a time, into your buffer. Then use strstr() to look for the target word, in the buffer. You don't want to do this, char by char.

To make it more efficient still, search for string search in Wikipedia, and read up. The best way, depends on how long your target word is.

Adak 419 Nearly a Posting Virtuoso

You can't compare strings - you have to compare each char, one at a time. Include string.h and use strcmp(string1, string2).

strcmp(string1Name, string2Name) will return one of three values:

1) a positive number
2) zero
3) a negative number

That return tells you what the comparison of strings revealed:

1) string1 is greater than string2
2) the strings are equal
3) string2 is greater than string1

The comparison is made, based on your system's character set, not strictly according to the dictionary rules of alphabetizing. (not quite a lexicographic comparison)

If you don't have a good C reference book or help files, bookmark a C reference site in your browser. You will need it.

Adak 419 Nearly a Posting Virtuoso

Try this:

//Before main() define a struct with 
struct countries {
  char name[50];
  int numNation;
}

//now make an array of these structs, in main() 
struct countries nations[500?];  
char myNation[50];

//and read in your data from a file, into the array of structs.

//Now a for loop can replace all the if else switch statements:

for(i=0;i<500?;i++) {
  if(nations[i].numNation = numBarcode) {
    strcopy(myNation, nations[i].name); //or print the country name here
    break;
  }
}

printf("%s\n", myNation);

;)

Adak 419 Nearly a Posting Virtuoso

Focus on one thing at a time - in this case, parsing the string.

Looks like -dH is the field identifier for your struct, where "H" represents a unique handle {w, Win, h, etc.}.

So make a list of these field id's: -dh, -dw, -dWin, etc., and use strstr() to return a pointer to them, wherever they are, in the string. Use that pointer to then assign the value they point to, to your struct fields.

You'll need to include <string.h> for strstr(), and use sscanf() to store your value (since it is formatted data) from a string). Make your buffer plenty large enough to handle one string - maybe BUFSIZ (which varies from system to system, but 512 chars size is common these days). (BUFSIZ is a macro).

Heaven help us, shared memory. You couldn't just pass the string to a function?

Anyway, one thing at a time, let's get the parse going locally, and then rest, later.

Adak 419 Nearly a Posting Virtuoso

I don't even know what the blazes you're doing with that code. *Argv[] is an array of pointers to char, so it should work fine with a string.

Forget code for a second. Show several strings as examples of your input. Then show the output that you want. Then show the pseudo code logic that you plan on using in your program to make that happen.

I know as a beginner, the C syntax can be a major hurdle, but ignore it for right now. Assume that you can get the syntax to work, with a little help, and it's not a problem. Concentrate on the logic, and program flow of that logic.

"Concentrate on the force, Luke!" ;)

Adak 419 Nearly a Posting Virtuoso

Yes, you did, and my warnings were not intended for your example, but for Pinkannu. I don't want to see code from the OP, until after the problem has been thoroughly studied, and the logic to solve it, has been found.

Adak 419 Nearly a Posting Virtuoso

PLEASE - don't start this with code - FORGET THE CODE FOR NOW!

Start with carefully studying the problem, in this case, the strings. KNOW those strings - know what are the separators between the fields, know how many fields they might have AT THE MOST.

STUDY & KNOW the problem, until you know how to solve it. Then set up the logic you used, to solve it. Put that logic into a flowchart or pseudo code, so each step of an algorithm to solve the problem, can be identified.

THEN write your code (which should spring right out from the pseudo code or flowchart).

Put off all the details, and get the flow of the program, and the functions you want for it, set up. Check your work as you go for any compiler warnings or errors, and for the accuracy of the program, so far.

Then add your details that you put off, and make any final little adjustments (typically, this will be to the look and feel of the interface).

Welcome to Top Down design! ;)

When you say:

A structure that assumes no name or value will be larger than 16 bytes

Who says you want char's? Why 16 bytes? That looks so much like a "magical number" you just pulled out of the hat.

Start with the strings, Luke! Turn off your target acquisition system in your X-wing fighter! ;)

Adak 419 Nearly a Posting Virtuoso

What is your field (struct member is also a field), separator? Hyphen, space, maybe both?

Here's a trick:
Initialize all your fields in the struct to 0 or '\0'.

Print out some strings. Put a vertical line between each field, with a pencil |.

Now you can loop through the buffer holding the string, as many times as you need to, parsing out one field at a time.

When it's all working for even the most difficult strings, then integrate the code enough to make it more efficient, but keep it clear enough that you can readily understand it.

You may need to change this code, if the string format ever changes in the future.

Adak 419 Nearly a Posting Virtuoso

I don't see anything obvious, but I'm not familiar with this, either.

If I were starting to debug it, I'd probably start with assertions to check that all the indices are staying within their proper ranges. Then start checking the subtotals, and whether j=0 is correct, for it's initialization (or should it be j=i?).
Should newmatrix be set to order-1?, etc.

There are lots of examples of this kind of code, but I don't know if it uses the cofactor method or not.

Adak 419 Nearly a Posting Virtuoso

Take in the telephone number as a string.

Scan over the chars, and remove any hyphens or /'s.

If any char in the string is unwanted, then

for(each char from that point, to the (end -1) of the string) {
   assign i+1 to i, in the array
}

and you're dancing on the ceiling. ;)

Adak 419 Nearly a Posting Virtuoso

In C, the name of the array, is ALMOST the same thing, as a constant pointer - and it points to the first element of the array - array[0].

So

char a[] = "Hello there.";

now the address of a, is the address of a[0], and a string operation includes all the char's until the end of string char is reached: '\0' or 0.

After the last e in a[] above, there would be an end of string char to mark it, even though you can't see it when the string is printed.

Adak 419 Nearly a Posting Virtuoso

How about a new line 11 1/2:

if(expression=='q') 
   break;

You should have a getchar() in your code, (maybe line 11 3/4ths, to remove the newline that scanf() always leaves behind.

Adak 419 Nearly a Posting Virtuoso

After EVERY scanf("%c...), when you are storing a char data type, add this:

getchar();

Right after it. OK, the last one in the whole program, you don't need to add this line of code.

What it does is remove the newline char that scanf() always leaves behind in the input stream (unless you use a special format).

This is THE most common problem for new students, in C. (Until they learn pointers) ;)

Adak 419 Nearly a Posting Virtuoso

Delete line 8, put lines 4 and 5, where line 8 is now. Right after the scanf() line, add this:

getchar();

//new line 5:
difficulty = '0';

And you don't need to assign diff in two places. Just assign it once, just before you return from the difficulty function.

I'm not sure what the whole "weak" "pro", and "low", "high" print out to the user is for. Either use one, or the other, but both seems quite redundant.

If you indent your subordinate code lines, your code will be easier to troubleshoot, by far.

One more thing - your choices are just two, difficulty and diff, being either l or h. So just

if(diff=='l') {
  //do something for low diff here
}else {
  //do something for high diff here
}
Adak 419 Nearly a Posting Virtuoso

You did not declare a prototype for your function. Default would be an int, which doesn't match what the compiler sees, when it gets down to the function.

Put your function prototypes above main(), with the right data types (the actual names of the variables are not needed), just int, char float, double, int *, char *, etc.

The "implicit declaration" is the declaration that the compiler is trying to make work, because you did not an explicit declaration of the function.

Adak 419 Nearly a Posting Virtuoso

Look at a matrix 2 x 2, with 0-3 for values:

0   1
2   3

When this matrix is turned 90° clockwise, where will each element wind up?

0 - goes to top row right column
1 - goes to bottom row right col
2 - becomes top row left col
3 - what does 3 become?

What relationships do you see in the above? The position of the value before the turn, let's call pos0. The position after the turn, let's call pos1.

pos1 = pos0 if column of the value, becomes the row. That is, the 0 column, becomes the 0 row, the 1 column (1 from the leftmost column), becomes what row?

Note that I'm always counting from the leftmost column, from left to right. That works fine when the rotation to be made is clockwise. If it's counterclockwise, then you start counting at the rightmost column, so that's the column that becomes column 0. The top row still is row 0, next row down is row 1, etc.

Work with a simple 2 X 2 array until you can see what I'm prattling on about here. Then try it with a 3 X 3 array, and work through it, this same way.

If you don't see it at first, keep working with it. The lightbulb WILL go off * POOF! *

;)

Adak 419 Nearly a Posting Virtuoso

Forget your code. Take a pencil and paper, and draw your array of 4 x 4 (or 2 x 2), whatever size you need. Make it simple as you need it to be.

Now, look at the paper from above, paper flat on the table or desk. Now slowly turn the paper 90° to the right. Repeat that until you have noted the 1st position of every element of the array, and it's last value, after the array is rotated.

<< THAT'S how you learn to solve a problem. >>

If you use a different value for every element of the array, it may help you.

Write down the equation that solves the change in position for any element, after the array has been rotated, given it's starting position. It's simple arithmetic, so no trig or calc is necessary, here.

Doing it this way, you WILL LEARN and remember, how this works, for a very long time. If I just told you, you'd forget it inside of a week.

And have fun, because this is a fun exercise. ;)

Adak 419 Nearly a Posting Virtuoso

Your indentation is horrid, of course. I believe your problem is that the scanf(), for a char especially, is troublesome, because scanf() always leaves the newline behind (when the user hits enter a newline is sent to the input buffer, as well as the keystroke he hits).

To remove the newline, add a getchar(), (or use any other means you want), to pull the newline off the input buffer. For multiple input problems, use the getchar() in a while loop, and pull off all the input char's until the char is a '\n' (a newline). That will be the last char, normally.

while((charVariable=getchar()) != '\n');

Note the semi-colon - this is a one line while loop.

Since the newline isn't seen, this always trips up beginners who rely on scanf() for user input. Another example of this can be seen where you have a do while(intVariable != 0), loop, which maybe shows a menu repeatedly. If you have no getchar(), and the user enters a single letter instead of a number -- well, it's not pretty. Try it. ;)

Adak 419 Nearly a Posting Virtuoso

Using a basic for or while loop, perhaps:

set i and j to zero
while(i is less than 12) {
  a[j]= index[i++]; 
  b[j] = index[i++];
  ... etc.
  j++;
}
Adak 419 Nearly a Posting Virtuoso

It should be obvious by now that this exercise requires SOME type of looping. Recursion is just a different way of looping.

Whether the looping works with the program code, (in whatever logic), the operating system, or even the underlying hardware, it makes no difference in terms of the logic. You can either write out the individual increments, one by one, or you will use some type of looping.

It's a great exercise for fun perhaps, but as far as teaching you something about C, it has minimal benefit, imo.

Adak 419 Nearly a Posting Virtuoso

Windows has always allowed programs to use the various ports and keyboard input. Can you do it without working through Windows on Win98 SE, -- I'm not sure.

If you're thinking about making a keylogger, stop right there. They have a serious risk of being misused - whether it's by you, or by someone else who stumbles upon the program. They have very little reason to exist that is not illegal, or immoral, and no one here will help you code one up. "Just to see if I can... just to study how it works...just to learn more about the system... NONE OF IT sounds credible in the least.

Give up on the keylogger idea, as fast as you can. They are nothing but trouble, even if you are a saint and would not abuse it -- because others would hear about it, or stumble across it, copy it, and then THEY would abuse it.

Adak 419 Nearly a Posting Virtuoso

First, use code tags around your program's code. Nobody wants to study code that looks like the dog's dinner, two days ago. ;)

Second, narrow it down. WHAT do you need help with? Are you getting the mean calculated OK?

Narrow down what we should focus on, and save us all a lot of time. You'd be amazed at the pure genius we see in noobs, with messing up a program!

And no matter how badly you want to avoid it, you WILL have to learn some troubleshooting techniques - and practice them. Your programs will initially have bugs, and you will have to hunt them down.

So what about the mean? Is that good?

Adak 419 Nearly a Posting Virtuoso

Edit: this seemed to work OK, in my very limited testing.

fp is the file pointer, ch is a char, and the other variables are int's. ctype.h was included, of course.

while((ch=getc(fp)) != EOF) {
    putchar(ch);
    if((isalpha(ch)) && (inword==0)) {
      inword=1;
      wordcount++;
    }
    else if(!isalpha(ch)) { //isalpha(ch)==0
      inword=0;
    }
  }
  fclose(fp);
  printf("\n I counted %d words in that file\n", wordcount);

Give this a try, and if no luck, post back, but I think you'll get it from this.

Adak 419 Nearly a Posting Virtuoso

Not yet - that's the same logic as before, using different variables. :(

in the header file ctype.h, we have functions to test what kind of char it is. I'd use two of them, so include <ctype.h> at the top of your program.

Those two are ispunct() (is it a punctuation, and isalpha(), is it a regular alphanumberic char (letter or number)?

So the logic will be (approximately):

if((inword  equals 0) and (isalpha(char being checked))) { //
  inword = 1
  wordcount increments by one
}
/* handles last word in a row which has no space after it. */
else if(char equals ' ' or !isalpha(char being checked)) {
  inword = 0
}

May be more logic needed, but not much. It's been a long time since I coded one up. Give this a try, and post back up and I'll study it more.

Adak 419 Nearly a Posting Virtuoso

Instead of the simple if(char == space) logic, (which obviously can't handle multiple spaces), how about this.

inword = 0; changes to inword=1 when you find a word. When you leave a word, it goes back to 0.

First letter you run across, you set it to 1, and increment word counter. When you run into the first space, you set it to 0, and do not increment word count any more, until you run into the next letter.

You need to add some logic to handle things like the last word in a sentence, at the end of a file, etc.

Adak 419 Nearly a Posting Virtuoso

I erred on the strlen(word)%2==0 idea. Not necessary, please drop it.

Here's your code, with a few tweaks. The tweaks are right below the code that needs tweaking:

#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include<string.h>
#include<ctype.h>
#include<dos.h>


/*All these global variables, let's remove and make them local variables instead. Seems a PITA maybe, but it pays big dividends in programming.

char word[289];
char *str1,*str2;
char *rev;
char *upper;
char *lower;
char *dup;
char choice[3];
int a,b,x,y,i,h,z,j,temp;
*/

//void main()
int main(void) //<===== Always int main, never void
{
 int len, i, j;
 char dup[250]; //nice and big :)
 char word[250];  //  ditto
 char choice;
 //input:  //use functions and logic, and not labels 95% of the time.
  
 //clrscr(); //this crashes my IDE, immediately
 printf("\n\n");  //gives the separation we need
 printf("ENTER A WORD:");
 gets (word); //unsafe! but we'll use it for now
 printf("\nYOUR WORD IS:");
 puts (word);
 //dup=strdup(word);
 strcpy(dup, word);
 len = strlen(word);
 //menu:
 //for(z=1;z<=5;z++)
 do 
 {
   printf( "\n\n1:REVERSE\n2:ACSENDING ORDER \n3:DESCENDING ORDER\n\4:UPPERCASE HIGHER HALF\n5:UPPERCASE LOWER HALF\n:");
  gets (choice);
  printf("\nYOUR CHOICE:");
  puts(choice);

  switch(choice[y])
  {
    case'1': {
      rev= strrev(word);
      printf("\n\t\t\t\REVERSE WORD:%s",rev);
		 getch();
      // clrscr();
      printf("\nYOUR WORD IS:");
      puts (dup);
      break;
    }
    case '2':{
     for(a=0;a<strlen(word);a++)
     {
       for(b=0;b<strlen(word);b++)
       {
	  if(word[a]<word[b])
          {
	     temp=word[a];
	     word[a]=word[b];
	     word[b]=temp;
	     printf("\n\t\t\t\RESULT:");
	     puts(word);
	  }
        }
      }

      getch();
      //clrscr();
      printf("\nYOUR WORD IS:");
      puts (dup);
      break;
    }
    case'3':{
      for(a=0;a<strlen(word);a++)
      {
		  for(b=0;b<strlen(word);b++)
		{
		if(word[a]>word[b])
		 {
		  temp=word[a];
		  word[a]=word[b];
		  word[b]=temp;
		  printf("\n\t\t\t\RESULT:");
		  puts( word);
		 }
		 }
		 }
		 getch();
		 clrscr();
		 printf("\nYOUR WORD IS:");
		 puts (dup);
	break;
       }
       case '4': {
	   if(strlen(word)%2==0)
		   {
		     for(i=0;i<strlen(word)/2;i++)
		     word[i]==toupper(word[i]);
		     {
		     for(h=0;h<strlen(word)/2;h++)
		     word[h]==tolower(word[h]);

		     printf("UPPERCASE …