Looping logic

N

Nicholas

At the end of the while loop, I would like to know that the integer values
are two, other symbol is one.
Integers: 3500 and 5
Other symbol: *

char*pt = "3500*5";
char numbuf[30];
int i;
// Counting the other symbols and integers
int others, integers;


Inside the function:

while (*ptr != '\0') {
if (isdigit(*ptr)) {
while (*ptr != '\0') { // Is it alright ?
// Need to 'peek' to the next one before -**- it if is integer
// If it is not integer, reset to the original ptr
numbuf = *pt;
i++;
ptr++;
}
integers++;
}
else // It is another symbol
other++;
ptr++; // -**-
}

printf("Integers: %d\n", integers);
printf("Other symbols: %d\n", others);

Thank you for the comments
 
M

Malcolm

Nicholas said:
char*pt = "3500*5";
char numbuf[30];
int i;
// Counting the other symbols and integers
int others, integers;


Inside the function:

while (*ptr != '\0') {
if (isdigit(*ptr)) {
while (*ptr != '\0') { // Is it alright ?
// Need to 'peek' to the next one before -**- it if is integer
// If it is not integer, reset to the original ptr
numbuf = *pt;
i++;
ptr++;
}
integers++;
}
else // It is another symbol
other++;
ptr++; // -**-
}

printf("Integers: %d\n", integers);
printf("Other symbols: %d\n", others);

Thank you for the comments

You need to present the code as a function.
eg
/*
counts the integers in a string, and also the number of other symbols
*/
void countintegers(const char *str, int *integers, int *symbols)
{
}
The you can test the code as a unit.

If your input is an decimal digit, presumably you want to skip all following
digits until you meet a non-digit, and then increment your integer count by
one.

Why are the integers being copied to numbuf? Do you want to convert them to
machine represntation? What happens if you are passed a string of over 30
digits?

You can use the function strtol() to simplify this code.
 
N

Nick Austin

At the end of the while loop, I would like to know that the integer values
are two, other symbol is one.
Integers: 3500 and 5
Other symbol: *

char*pt = "3500*5";
char numbuf[30];
int i;
// Counting the other symbols and integers
int others, integers;


Inside the function:

while (*ptr != '\0') {
if (isdigit(*ptr)) {
while (*ptr != '\0') { // Is it alright ?
// Need to 'peek' to the next one before -**- it if is integer
// If it is not integer, reset to the original ptr
numbuf = *pt;
i++;
ptr++;
}
integers++;
}
else // It is another symbol
other++;
ptr++; // -**-
}

printf("Integers: %d\n", integers);
printf("Other symbols: %d\n", others);


I'm not sure what you're trying to do with numbuf[], however
the following will produce a count of the number of integers
and non-integer characters:

while ( *ptr != '\0' )
{
if ( isdigit( *ptr ) )
{
while ( isdigit( *ptr ) )
{
ptr++;
}
integers++;
}
else
{
ptr++;
others++;
}
}

Nick.
 
N

Nicholas

char*pt = "3500*5";
char numbuf[30];
int i;
// Counting the other symbols and integers
int others, integers;


Inside the function:

while (*ptr != '\0') {
if (isdigit(*ptr)) {
while (*ptr != '\0') { // Is it alright ?
// Need to 'peek' to the next one before -**- it if is integer
// If it is not integer, reset to the original ptr
numbuf = *pt;
i++;
ptr++;
}
integers++;
}
else // It is another symbol
other++;
ptr++; // -**-
}

printf("Integers: %d\n", integers);
printf("Other symbols: %d\n", others);


I'm not sure what you're trying to do with numbuf[], however
the following will produce a count of the number of integers
and non-integer characters:

while ( *ptr != '\0' )
{
if ( isdigit( *ptr ) )
{
while ( isdigit( *ptr ) )
{
ptr++;
}
integers++;
}
else
{
ptr++;
others++;
}
}

Nick.


Hi Nick. After exiting from the second while loop, I need numbuf[] to group
the integer, so in this case of 3500
numbuf would contain "3500". My code gave me core dump

Main:
char *str = "3500*5";
function(str);

/*** ***/
void function(char *strg) {

char *ptr = strg;

while ( *ptr != '\0' ) {
if ( isdigit( *ptr ) ) {
// Resetting numbuf
numbuf[5] = (char) "";
while ( isdigit( *ptr ) ) {
ptr++;
numbuf[index] = *ptr;
index++;
}
printf("The integer is: %s", *numbuf);
integers++;
}
else {
ptr++;
others++;
}
}

Thank you for the comments
 
S

Stephan Wilms

Hi Nicholas,

Have a look at the following example. It uses one outer loop that
iterates through the characters of the string. An int is detected
whenever a digit char is followed by a non digit char. The function
returns 0 on success and non zero upon error.

int count( char *str, int *pNumInts, int *pNumOther )
{
char *pCurChar;
char numbuf[300];
int i = 0;

/* initialise counters and protect against array overflow */
*pNumInts = *pNumOther = 0;
if ( strlen(str) >= sizeof numbuf )
return 1;

for ( pCurChar=str ; *pCurChar != '\0' ; pCurChar++ )
{
if ( isdigit(*pCurChar) )
{
numbuf[i++] = *pCurChar;

/* we found an int if next char is not digit */
if ( !isdigit(*(pCurChar+1)) )
{
*pNumInts += 1;
numbuf = '\0';
printf( "found the int '%s', count = %d\n", numbuf,
*pNumInts );
i = 0;
}
}
else
{
*pNumOther += 1;
}
} /* for */

return 0;
}
 
A

Aishwarya

Hi Nicholas,

Just had a look at the problem and I gather that the aim is two fold :

1. Count the number of integers and non-integers in a string.
2. seperate the values of the integers for future use.

(Please correct me if I am mistaken).

Can we not extend Nick's program to get the integers out ? How abt the
following code ?


#include <stdio.h>
#include <stdlib.h>

int main()
{
char *ptr="3500*5";
int numbuf[5]; /* I am reducing numbuf to have just 5 elements as we
do not need to store more values in the problem*/
int i=0;
int othersCount=0, IntCount=0;

while ( *ptr != '\0' )
{
if ( isdigit( *ptr ) )
{
numbuf[IntCount]=atoi(ptr);

while ( isdigit( *ptr ) )
{
ptr++;
}
IntCount++;

}
else
{
ptr++;
othersCount++;
}
}

printf("integer count=%d\ncharacter count=%d\n",IntCount,
othersCount);

for(i=0;i<IntCount;i++)
printf("integer number %d is %d\n", i+1, numbuf);

return(0);
}



so at the end, the output should have numbuf having values of the two
integers in the first two elements , integers having the count of
integers (which is two in this case) and others having the count of
non-integers (which is one in this case).

thoughts / comments guys?

Cheers
Aish

PS: you used pt and ptr as two pointers, I assume that they refer to
the same variable ..
 
A

Artie Gold

Nicholas said:
char*pt = "3500*5";
char numbuf[30];
int i;
// Counting the other symbols and integers
int others, integers;


Inside the function:

while (*ptr != '\0') {
if (isdigit(*ptr)) {
while (*ptr != '\0') { // Is it alright ?
// Need to 'peek' to the next one before -**- it if is integer
// If it is not integer, reset to the original ptr
numbuf = *pt;
i++;
ptr++;
}
integers++;
}
else // It is another symbol
other++;
ptr++; // -**-
}

printf("Integers: %d\n", integers);
printf("Other symbols: %d\n", others);


I'm not sure what you're trying to do with numbuf[], however
the following will produce a count of the number of integers
and non-integer characters:

while ( *ptr != '\0' )
{
if ( isdigit( *ptr ) )
{
while ( isdigit( *ptr ) )
{
ptr++;
}
integers++;
}
else
{
ptr++;
others++;
}
}

Nick.



Hi Nick. After exiting from the second while loop, I need numbuf[] to group
the integer, so in this case of 3500
numbuf would contain "3500". My code gave me core dump

Main:
char *str = "3500*5";
function(str);

/*** ***/
void function(char *strg) {

char *ptr = strg;

while ( *ptr != '\0' ) {
if ( isdigit( *ptr ) ) {
// Resetting numbuf
numbuf[5] = (char) "";


Because of the snipping, it's hard to see what's going on here...but
I sincerely doubt the above assignment does what you think it does.

[Hint: you've cast the address of a string literal to a char]
while ( isdigit( *ptr ) ) {
ptr++;
numbuf[index] = *ptr;
index++;
}
printf("The integer is: %s", *numbuf);
integers++;
}
else {
ptr++;
others++;
}
}

Thank you for the comments
--ag
 
W

W.Paure

Hi Nicholas,

Just had a look at the problem and I gather that the aim is two fold :

1. Count the number of integers and non-integers in a string.
2. seperate the values of the integers for future use.

(Please correct me if I am mistaken).

Can we not extend Nick's program to get the integers out ? How abt the
following code ?


#include <stdio.h>
#include <stdlib.h>

int main()
{
char *ptr="3500*5";
int numbuf[5]; /* I am reducing numbuf to have just 5 elements as we
do not need to store more values in the problem*/
int i=0;
int othersCount=0, IntCount=0;

while ( *ptr != '\0' )
{
if ( isdigit( *ptr ) )
{
numbuf[IntCount]=atoi(ptr);

while ( isdigit( *ptr ) )
{
ptr++;
}
IntCount++;

}
else
{
ptr++;
othersCount++;
}
}

printf("integer count=%d\ncharacter count=%d\n",IntCount,
othersCount);

for(i=0;i<IntCount;i++)
printf("integer number %d is %d\n", i+1, numbuf);

return(0);
}



so at the end, the output should have numbuf having values of the two
integers in the first two elements , integers having the count of
integers (which is two in this case) and others having the count of
non-integers (which is one in this case).

thoughts / comments guys?

Cheers
Aish

PS: you used pt and ptr as two pointers, I assume that they refer to
the same variable ..

I agree this. I understand that meaning of orange writer is this.
 
D

Dave Thompson

Hi Nick. After exiting from the second while loop, I need numbuf[] to group
the integer, so in this case of 3500
numbuf would contain "3500". My code gave me core dump

Main:
char *str = "3500*5";
function(str);

/*** ***/
void function(char *strg) {

char *ptr = strg;
You don't declare numbuf. You say it should contain "3500", a string,
so I assume it is char numbuf [N] where N is at least 5.
while ( *ptr != '\0' ) {
if ( isdigit( *ptr ) ) {

Better isdigit( (unsigned char) *ptr ), and similarly below, for
(portability to) systems where plain char is signed.
// Resetting numbuf
numbuf[5] = (char) "";

This makes no sense. (char) "" is some arbitrary, effectively random,
address, converted to a char, which may fail if char is signed and in
any case is not a useful value. And even if you instead used '\0' (or
just 0), storing to numbuf[5] is likely useless since your loop below,
for the input above, will not fill beyond numbuf[3], leaving at least
one intermediate byte uninitialized if numbuf is automatic; and may
well be illegal if numbuf has bound 5.

If you want to set all the bytes of numbuf to zero, use memset
(numbuf, 0 /* or '\0' */, sizeof numbuf /* or 5 or whatever */) or
strncpy (numbuf, "", sizeof numbuf) or write out a loop.

But you don't need to do this to create a valid string; see below.
while ( isdigit( *ptr ) ) {
ptr++;
numbuf[index] = *ptr;
index++;
}

If you want numbuf to contain a valid string, it is sufficient to here
append *one* null terminator character: numbuf[index] = '\0';
printf("The integer is: %s", *numbuf);

Assuming numbuf is char[], this is the wrong type; it tries to print a
single character as a string, which probably causes your core dump.
integers++;
}
else {
ptr++;
others++;
}
}

Thank you for the comments

- David.Thompson1 at worldnet.att.net
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,079
Messages
2,570,574
Members
47,206
Latest member
Zenden

Latest Threads

Top