yellowSnow 607 Posting Whiz in Training

Hi Troy,

Thanks for your reply. Unfortunately it does not work. The javascript interpreter throws an error on your code . Doesn't the character class 'd' have to be escaped with "\"? Anyway, I tried your code using "/\d+/g" and I get the same result as I reported in my original post. I am using 'w' because there is potential for alpha characters to appear in the input (although my test data only shows digits).

Thanks,
JD

yellowSnow 607 Posting Whiz in Training

Hello,

I am trying to parse a string using regular expressions. This string can potentially have any number of non-alphanumeric characters acting as delimiters. These delimiters can also be grouped together. Here are some examples of the data:

00923:5342:123 --> I want to extract 00923, 5342 and 123 into an array

08765::764375 --> parse this as 08765 and 764375 into an array

3246//672354//23458 --> parse as 3246, 672354 and 23458

23564-73654 --> etc

I'm using the following code:

var ids = str.match(/\w*/g);

But it just returns the first token from each line and ignores the rest. So with the above data I'm only getting back 00923, 08765, 3246 and 23564 into the first element of the "ids" array.

I've also tried an "inverse" approach using the split method like so:

var ids = str.split(/\W*/g);

and I just get the same result - the first token only.

My test code looks like this ('str' contains a string read in from a file):

var ids = str.match(/\w*/g);
//var ids = str.split(/\W*/g);

task.logmsg("the length of the array is " + ids.length);

for (i = 0; i < ids.length; i++) {
    task.logmsg("ids=[" + i + "]=" + ids[i]);
}

I can confirm that ids.length is returning 1 (??)

The 'task' object comes from a separate library and the 'logmsg' method just writes to the console.

My reptilian brain is struggling with reg exps. This shouldn't be difficult to do, but I just can't get it to work.

If anyone out there can lend …

yellowSnow 607 Posting Whiz in Training

Hmm...no man, that only makes it a LOT worse, but thanks for trying. I appreciate it. Here's some rep ;)

At any rate, if you haven't noticed, this is a forum on C not C++. You should post your questions in the correct forum.

yellowSnow 607 Posting Whiz in Training

The modulus operator (%) requires integral data types for its operands. At any rate x % 1 is zero.

yellowSnow 607 Posting Whiz in Training

can you please help me by elaborating this.i mean with full program

You need to show some effort - you will not get answers for "free". Read this:
http://www.daniweb.com/forums/announcement118-2.html

yellowSnow 607 Posting Whiz in Training

Post your code - or at least the minimal amount that compiles and illustrates your issue.
Also, "if" is not a function.

yellowSnow 607 Posting Whiz in Training

Here's what I have so far. I am trying to enter a number and convert the number entered into hours, minutes and seconds. I must use "pass by reference" in my code.

#include<stdio.h>

void time(int, int*, int*, int*); //Function Prototype

int main(void)  //function header


{
    int hold;
    float total;
    
    
    printf("Enter the number of seconds ->");
    scanf("%d",&total);
    scanf("%d", &hold);
    return 0;
}  // end of main function

    void time (int total, int *hours, int *min, int *sec) // Function header Line
       {
           int rem1, rem2;
          *hours = total / 3600;
           rem1 = total % 3600;
           *min = rem1 / 60;
           rem2 = rem1 % 60;
         printf ("the number of hours is ->", *hours);
         printf ("the number of minutes is ->", *min);
         printf ("the number of seconds is ->", *sec);  
        }

Here are some observations on the code you have posted.

- I'm not sure why you have declared the variable hold. I'm assuming that the variable total is supposed to be the total number of seconds that will be the input to your time function. That being the case, then it should be declared as int.

- In main you don't even call your time function. You haven't declared variables for hours, minutes and seconds as well. These should be declared and their addresses should be passed to the time function you have defined (there's your pass by reference).

- In your time function, you haven't calculated the number of seconds and you don't need the rem2 variable …

yellowSnow 607 Posting Whiz in Training

ok i have made a calculator using turbo c but it keeps disappearing when i choose the run command can any body help me please

Well congratulations on bumping a five year old thread. In future, start a new thread.

yellowSnow 607 Posting Whiz in Training

Hi
Modify this where possible

And what exactly do you want to modify? Ask a specific question.

yellowSnow 607 Posting Whiz in Training

I want to write a C program to test whether the character is uppercase, lowercase, digit or special symbol.

void main ()
{

char ch;

printf("\n Enter any character from the keyborad");

scanf("%c",&ch);

if (ch>=65&&ch<=90)

printf(" \n the character is uppercase);

if(ch>=91&&ch>=122)

printf(" \n the character is lowercase);

if(ch>=48&&ch<=57)
printf("\n You entered a digit );

}

My question how to print whether the character is a special symbol. I don't know this. Please can anyone point out.

Thanks

This code is specific to the ASCII character set. It would not work with other character sets. There are functions available such as isupper(), islower(), isdigit() etc that you can use for this exercise. These functions are available with the ctype.h header file.

yellowSnow 607 Posting Whiz in Training

Well, can't you just change the algorithm you used for the limit of 20 and adjust it to 100000? Perhaps you can post the code you have already.

yellowSnow 607 Posting Whiz in Training

alloca is similar to malloc in that you can allocate memory dynamically, however the memory is automatically freed when you exit the function that called alloca.

With an array you can't compute the storage required at runtime.

yellowSnow 607 Posting Whiz in Training

Finally, you may want to consider a performance improvement to your prime checking function with regards to the termination condition in the for loop. There is no need to check for divisors up to the number passed - you can just check for divisors between 2 and the square root of the number. For example:

for (div = 2; div <= sqrt(n); div++)

Errh my bad ... you probably should calculate the square root first and use the result in your for loop:

double root = sqrt(n);
for (div = 2; div <= root; div++) {
    if (n % div == 0)
        return 0;
}

This prevents the square root from being calculated on each iteration of the loop.

yellowSnow 607 Posting Whiz in Training

Do you mean that you want to print 10 primes per line? If so, then use the col variable to count the number of primes that have been output for the current line. Check the count and if it's 10, print the newline character and reset col to 0.

for (x = 1; x <= 1001; x++) {
    if (isprime(x)) {
        printf("%3d  ", x);
        col++;
        if (col == 10) {
            printf("\n");
            col = 0;
        }
    }
}

Also you need to correct your isprime() function to cater for the special case of 1 being passed - 1 is not prime.

Finally, you may want to consider a performance improvement to your prime checking function with regards to the termination condition in the for loop. There is no need to check for divisors up to the number passed - you can just check for divisors between 2 and the square root of the number. For example:

for (div = 2; div <= sqrt(n); div++)

There are other ways to improve the performance of the function but this suggestion is a good start. If you go down this path, you will need to include the math.h header file (for the sqrt() function).

You may also want to consider what to do if a negative number is passed into the isprime() function.

yellowSnow 607 Posting Whiz in Training

use getch()

Just be wary that the getch() function and the associated conio.h header file are not portable.

yellowSnow 607 Posting Whiz in Training

Can you explain this last line. I understand the bit wise 'and' to isolate the bit, but I am lost on the last line. (return statement)

return (*ip & 0x80000000 ? 1 : 0);

Thanks for your help

The example I posted demonstrates the use of a ternary operator. Here's a brief summary on how the ternary operator works:

http://cprogramminglanguage.net/c-ternary-operator.aspx

Essentially it is a short form equivalent of the if-else construct. I posted it this way because your original post stated that you weren't permitted to use any 'if' type statements.

Have a read and post back if you still have questions.

yellowSnow 607 Posting Whiz in Training

Mmm ... interesting. I always thought that bitwise operators were only valid with integral operands. The only way I can think of using bitwise operators with floats/doubles is to use casting pointers. Perhaps something like:

int isNegative(float f) {
    int *ip = (int*)(&f);
    return (*ip & 0x80000000 ? 1 : 0);
}

But then, this may also be too complex for a new programmer.

yellowSnow 607 Posting Whiz in Training

I actually had a bad experience with that when A classmate stole my work turned it in and i got in trouble :(

I'm sorry that you had this experience.

But it's going to be awfully difficult for anyone here to help without some code to look at. At least provide more specific information on your issue with a minimal amount of code that demonstrates your problem.

yellowSnow 607 Posting Whiz in Training

you can use code blocks it's cool

I concur - Codeblocks is good.

But I use Eclipse far more often as it has support for other languages that I use (for work and for play). So for me, it's a "one-stop" shop as far as an IDE is concerned. And it's not too bad to configure for C development. I use the MinGW toolchain in combination with gdb. The debugger has some minor issues that should hopefully be sorted out soon - but generally speaking the set up works well enough.

yellowSnow 607 Posting Whiz in Training

Why are you even posting this piece of crap as a code snippet? Code snippets are usually posted by more knowledgeable members of this forum who have something useful to share with the rest of the community. That piece of rubbish should have been posted as a normal thread and more importantly a specific question should have been asked.

Before posting a code snippet, take a look at the real good snippets that have been posted and learn from it.

yellowSnow 607 Posting Whiz in Training

HI ,
I am a student in an university...
I am assigned a project about the knight's tour on the chess , but must use the Warnsdorff's rule ...
rule : Put a knight on the chess with any square , and find all patchs of knight (knight pass each square only once time )..
Who can help me???
Thanks lot...
SNIP
Well it's a minor miracle that you even got into university. You obviously didn't read this:
http://www.daniweb.com/forums/announcement118-2.html
I suppose you were hoping that some programming fairy was going to come along and sprinkle some magical fairy dust over your problem and woosh ... all answers would be revealed. You're just lazy mate.

Show some effort; read the material on the above link and try again.

yellowSnow 607 Posting Whiz in Training

Oh for crying out loud. Stop being such a cry baby. You have posted enough messages now to know that you should be using code tags.

WaltP has kindly asked you on at least two occasions to use tags and you just choose to ignore the advice.

Since you're too bloody lazy to read the sticky threads on the opening page of this forum, here's a link on how to post code using tags.

Read it, apply it and you'll be surprised to find out how willing people on this forum will be to help you out if you show some friggin' effort. If you can't manage a simple exercise such as this, then you're in the wrong game.
http://www.daniweb.com/forums/announcement118-3.html

yellowSnow 607 Posting Whiz in Training

And what do you want us to do you? You're just plain lazy. Do some work and show some effort. You obviously haven't read this sunshine:
http://www.daniweb.com/forums/announcement118-2.html

yellowSnow 607 Posting Whiz in Training

Like so many other ignorant newbies that come to this forum, you obviously haven't read this:
http://www.daniweb.com/forums/announcement118-2.html

Tom Gunn commented: Very rude. +0
kvprajapati commented: No. +6
yellowSnow 607 Posting Whiz in Training

Using scanf for strings, you will run into trouble if your input string contains whitespace. Only the first word will be reversed.

Also, in your reverse function, you are assigning the null incorrectly - it should be:

p[i] = '\0';

and not

s[i] = '\0';

Also, you should probably use another char* variable (as the return value) for your call to the reverse function in main. That way you don't destroy the original input string and you don't end up with a memory leak. Remember to free any dynamic allocated memory as well.

yellowSnow 607 Posting Whiz in Training

help me make a payroll system for turbo c please????????
i really need the code for today plss :((

Are you guys eternally deranged or you just don't know how to read!

Oh yeah right, no specifiications, no requirements and you want the code for a payroll system today. In addition to this - NO FRIGGIN' EFFORT shown.

Mate, you're out with the fairies.

yellowSnow 607 Posting Whiz in Training

I would probably store your times in int variables. This variable would store the times in seconds. Then for the final result, I would divide by 60 to get the number of minutes and use the modulus operator to get the seconds.

If you need to store fractions of seconds, then your int variable (or perhaps long) would represent the number of milliseconds or number of centiseconds depending on the accuracy required.

yellowSnow 607 Posting Whiz in Training

In your haste to getting a quick answer you obviously didn't read this:
http://www.daniweb.com/forums/announcement118-2.html

yellowSnow 607 Posting Whiz in Training

With the char* string case you're trying to modify a string literal.
This link to the C FAQ explains it better:
http://c-faq.com/decl/strlitinit.html

yellowSnow 607 Posting Whiz in Training

pls reply !!

Here's your reply ... READ THE PREVIOUS POSTS IN THIS THREAD!
And you obviously need to read this:
http://www.daniweb.com/forums/announcement118-2.html

and this:
http://www.daniweb.com/forums/thread78060.html

Unbelievable!

yellowSnow 607 Posting Whiz in Training

Ok here we go:

- you're missing the semi-colon after your declaration of the variable n.

- you haven't declared the variables i and count. And you don't need the count variable. Just print the value of i.

- remove the semi-colons from the end of your for statements - you have empty for statements here and that is why you are not getting the desired output.

- when counting back, initialize i to n-1.

- the code to print the required output should be part of an else clause - that way, if invalid data is entered, you don't end up printing erroneous values after printing your error message.

- in the future, use code tags when posting code - read the information at this link to learn how to do this:
http://www.daniweb.com/forums/thread93280.html

yellowSnow 607 Posting Whiz in Training

What should you do? Learn Java. It's going to be difficult to do the translation if you don't know the language you're translating to or from.

At any rate, a line-by line translation is not going to be possible as you will have to write equivalent C code to emulate the Java classes used e.g. ArrayList.

yellowSnow 607 Posting Whiz in Training

You're over-complicating this a bit. Have a look at the following snippet and see if you can follow what it does:

for (j = NUMBEROFBITS-1; j >= 0; --j) {
        if (i & (1 << j))
            putchar('1');
        else
            putchar('0');
    }

NUMBEROFBITS is defined as the number of bits in an int for your system - for example let's say it's 32. The variable i is your input value.

yellowSnow 607 Posting Whiz in Training

Try:

sprintf(myStrings, "%02X%02X%02X%02X", myHexas[0], myHexas[1], myHexas[2], myHexas[3]);

And use unsigned char for your data type.

yellowSnow 607 Posting Whiz in Training

uhm .. Good day everyone. I would just like to ask if anyone knows what code to use in Turbo C wherein the user will input the year and the program will output the calendar for the whole year. Please help me. Thanks a lot :)

You obviously haven't taken the time to read this:
http://www.daniweb.com/forums/announcement118-2.html

yellowSnow 607 Posting Whiz in Training

but in class we havent used floats yet. I saw some examples on internet but they were too advanced for the class i am in right now. We're just in the basics. we went through only printf, int /double functions and scanf and if statements lol

Well then, change your scanf's as so:

scanf("%d%d%d",&x,&y,&z);

Also, in your code snippet, you are not calling the function - perhaps something like this may help:

printf("The result is %lf\n", vectorDotProduct(a, b, c, x, y, z));
yellowSnow 607 Posting Whiz in Training

I'm in trouble with data structures mam..
i want to finish my lab work soon so please any gals/guys help me..
i need c coding in expression tree...

In your haste to get a quick answer to your very poorly worded question, you have missed reading this:

http://www.daniweb.com/forums/thread78060.html

Addressing people as "mam" is not helping your cause as well.

yellowSnow 607 Posting Whiz in Training

A quick reply from the "style police":

Use code tags please and repost:

http://www.daniweb.com/forums/thread93280.html

yellowSnow 607 Posting Whiz in Training

Hey guys I am stuck with this program and i need help with it
this is what i got until now

#include <stdio.h>
#include "checkit.h"

double vectorDotProduct(double x1, double y1, double z1, double x2, double y2, double z2) 
{
	return(x1 * x2 + y1 * y2 + z1 * z2);
}
int main (void)
{
  int x,y,z,a,b,c;
  printf("Please enter the x, y, and z coordinates for the first vector: ");
  scanf("%f%f%f",&x,&y,&z);
  printf("Please enter the x, y, and z coordinates for the second vector: ");
  scanf("%f%f%f",&a,&b,&c);
  
  return 0;
}

this is for my hw but I am absolutely stuck on this one. Im trying to get help but its not working out.

A first look would suggest that you have declared variables a, b, c, x, y and z as integers and scanning in the scanf() function call for these variables as floats.

yellowSnow 607 Posting Whiz in Training

When the program is given a multicast address it should
print the corresponding mac address, along with a list of the 32
overlapped multicast ip addresses.

./ipcalc.pl 224.1.1.1 255.255.255.255
Class D (Multicast) Address: 01-00-5e-01-01-01
Overlapped Addresses
224.1.1.1
224.129.1.1
225.1.1.1
225.129.1.1
226.1.1.1
226.129.1.1
227.1.1.1
227.129.1.1
228.1.1.1
228.129.1.1
229.1.1.1
229.129.1.1
230.1.1.1
230.129.1.1
231.1.1.1
231.129.1.1
232.1.1.1
232.129.1.1
233.1.1.1
233.129.1.1
234.1.1.1
234.129.1.1
235.1.1.1
235.129.1.1
236.1.1.1
236.129.1.1
237.1.1.1
237.129.1.1
238.1.1.1
238.129.1.1
239.1.1.1
239.129.1.1

You are obviously living in a dream full of fairies and candy if you think that anyone is going to help you with your so called question.

What the hell is going on with the quality of the posts this past week or so?

yellowSnow 607 Posting Whiz in Training

i don't have code.simple plz

Here we go again - another lazy poster. If it's so simple then work it out for yourself.

You obviously haven't taken the time to read the rules and ethics on how to post to this forum.

yellowSnow 607 Posting Whiz in Training

start quote:

****                       *
***        and         **
**                       ***
*                       ****

well my teacher wont say a thing and have passed this act.
now our teacher is a different one and i can't still figure it out..
help pls??...

:S

the only loop i know is this

*
**
***
****
===============================================
#include<stdio.h>
main()
{
int x,y,size;
clrscr();
{
printf("Input Size:");
scanf("%i",&size);
for(x=0;x!=size;x++,printf("\n"))
for(y=0;y!<=x;y++)
printf("%c",1);
}
getch();
}
===============================================

end quote.

You just don't get it sunshine.

You ask for help without any real effort. You have chosen to ignore previous advice about the use of code tags and sensible thread titles.

You've made your bed ... you sleep in it!

Good luck - you're going to need it.

yellowSnow 607 Posting Whiz in Training

aahahaha!

im not really in on reading rules

sorry!


im a newbie and just first year in my class so sorry to all disappointment!


^_^!

im glad to join this forum!!

Mmm ... you're not really into reading rules. There are many members of this forum who won't be "really into helping you out" with that attitude. You'll get no "sweet sympathy" here with that modus operandi. Being a newbie and in first year is no excuse ... it just sounds like you're plain lazy.

Trust me; if you keep those types of responses going, you'll be subconsciously blacklisted before you know it (meaning - most members will choose to ignore you in the future). It's working for me already.

Going by your other posts, you may also take a little time to give your posts appropriate titles as well ... "here's the code" and "plz help" are crap titles for threads.

yellowSnow 607 Posting Whiz in Training

error was simple. The function was encapsulated into main rather than outside. However, my desired result is not coming out. Now I need some suggestions with the actual code. Thanks!

Gees, you really like doing things the hard way. Here's a simple code snippet that outputs the character:hex representations of each character of a string. See if you can incorporate some aspects of the code into your project.

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

int main(void) {

    char str[] = "ABCDEFG";
    int i;

    for (i = 0; i < strlen(str); ++i) {
        printf("%c:%x ", str[i], str[i]);
    }

    return 0;
}
yellowSnow 607 Posting Whiz in Training

Please can somebody explain to me whether sometimes the strrev function fails or not.
I was giving an assignment to write an application that can detect whether an entered word from the keyboard is a palindrome or not.
After accepting the string and tried to use the function from the string.h.

You don't necessarily need a "string reverse" function to check for palindromes.

For a string of length N, how about:
1. check if the 1st and Nth character are equal, if so, then:
2. check if the 2nd and (N-1)th character are equal, if so, then:
3. check if the 3rd and (N-2)th character are equal,
.......etc etc.

If any of the conditions fail, then you don't have a palindrome.
This can be done iteratively or recursively.

yellowSnow 607 Posting Whiz in Training

I suppose you still believe in the tooth fairy, Father Christmas and the Easter Bunny.

No one - and I mean no one is going to help you out based on the effort you have put in.

Read this:
http://www.daniweb.com/forums/announcement118-2.html

and this:
http://www.daniweb.com/forums/thread93280.html

and then this:
http://www.daniweb.com/forums/thread78060.html

I'll bet London to a brick, we won't here from you again (except perhaps for some whinge).

kvprajapati commented: I appreciate your suggestion. +18
yellowSnow 607 Posting Whiz in Training

Create two int variable.
Ask user for input
Check if input is valid
if so the proceed else go back to get user input
start a for loop from i = 0; i < L;
start another loop ( nested loop ) that ranges from j = 0; k < H
print out a symbol, in your case its an "*";
after the second nested loop is done, print a new line character to start
the new process in a different line.

Then you are done.

Just one pedantic point.
The ordering of the loops should probably be:

loop from 0 to < H
    loop from 0 to < L (nested loop)

Cheers,
JD

yellowSnow 607 Posting Whiz in Training

ok my bad I found out there was trailing crap from a previous scanf that was bunging things up.. and that flush is useless.... but I may have more questions.. so I want mark te thread as solved yet.

You're making this more difficult than it needs to be and fflush(stdin) is undefined behaviour according to the standard (although some compilers probably support it).

Take a look at the code snippet below (minus the GetAChar() function):

#include <stdio.h>

void MainMenu() {
    char selection = '\0';
    do {
        printf("Bla Bla.. ");
        printf("Please make your selection (1, 2, 3 or 4): ");
        //selection = GetAChar();
        selection = getchar();
        switch (selection) {
            case '1':
            case '2':
            case '3':
                printf("You selected %c\n", selection);
                break;
            default:
                printf("Whatever!\n");
                break;
        }
        // consume any additional chars entered up to and including
        // the newline character
        while (getchar() != '\n');

    } while (selection != '4');

    return;
}

int main(void) {

    MainMenu();
    return 0;
}

Try the code out and see if you can incorporate aspects of it into your project.

yellowSnow 607 Posting Whiz in Training

How about showing us what you have done so far? That would at least give people on this forum a good starting point as to how your problem could be resolved. It would also give us some indication as to your level of knowledge in C in general.

yellowSnow 607 Posting Whiz in Training

First up, as a newbie, you need to learn how to post code correctly on these forums using code tags. You'll find that people will be more willing to help you out if you're prepared to show some effort in this area. Please read the information at the following link to learn how to use code tags:
http://www.daniweb.com/forums/thread93280.html

Now moving on to your code. In addition to the anomaly that Samyx has pointed out, here are some other things to consider:
1. When using scanf() with strings, the address-of operator (&) is not required. You're doing this when scanning input for the student names.

2. While on the subject of the address-of operator, you're using them in the for loop that prints out student details - these are not required.

3. You have declared two variables (i and j) for use as loop variables. Only one is required (drop j).

4. You have a bug in the code that prints out first-class student details. You use the variable j in your loop statement and then use the variable i to reference your array elements. As in point 3, drop the j variable and change the code here accordingly.

5. A minor point - you can probably lose the else clause - the requirement only asks that you print the details for those students that are "first-class".

6. Try to avoid the use of "magic" numbers - you should probably declare constants …

kvprajapati commented: Very good suggestion. +17