estracting single characters in a string

L

Lampa Dario

Hi,

I need to estract single characters in a string, that may be '0' or '1',
and evaluate them.

I defined these variables

char *binary;
char cypher;
int value;

then
binary=malloc(32);
scanf("%s",binary);
n=strlen(binary);

If I write

for (i=0;i<n;i++)
{
strcpy(cipher,binary);
value=atoi(cipher);
....
}

I get a compilation error.

If I write

cypher=atoi(binary);

I get another compilation error .

What is the correct procedure to evaluate every single char in a string?

At the end I wrote
value=binary-'0'

and everything worked fine.

Francesco
What I need is to evaluate all the 0s and 1s in the string
 
N

Nick Keighley

Lampa Dario wrote:

I've cleaned up your code
I need to estract single characters in a string, that may be '0' or '1',
and evaluate them.

I assume you put these in

#include <string.h>
#include <stdlib.h>

int main (void)
{
char *binary;
char cypher;
int value;

binary = malloc (32);
scanf ("%s", binary);
n = strlen(binary);

for (i = 0; i < n; i++)
{
strcpy (cipher, binary ); expects two char*s
value = atoi(cipher);
/* ... */
}
}
I get a compilation error.

yup. strcpy(), as its name suggests, copies a string (an array of char)
you tried to copy a char.
If I write

cypher = atoi (binary);

I get another compilation error .


surprise. atoi() takes char* and returns an int. binary isn't a
char*.
What is the correct procedure to evaluate every single char in a string?

I don't know what you mean by "evaluate". If you want extract a single
char just index the array, binary.
At the end I wrote
value=binary-'0'

and everything worked fine.


so you solved your problem. Why the post?

What I need is to evaluate all the 0s and 1s in the string

what does a typical string look like and what does "evaluate" mean?
 
L

Lampa Dario

#include said:
#include <stdlib.h>

int main (void)
{
char *binary;
char cypher;
int value;

binary = malloc (32);
scanf ("%s", binary);
n = strlen(binary);

for (i = 0; i < n; i++)
{
strcpy (cipher, binary ); expects two char*s value =
atoi(cipher);
/* ... */
}
}
}
I get a compilation error.

yup. strcpy(), as its name suggests, copies a string (an array of char)
you tried to copy a char.


Well, how do I copy a char, only writing
c=binary ?
surprise. atoi() takes char* and returns an int. binary isn't a char*.
Ok
What is the correct procedure to evaluate every single char in a string?

I don't know what you mean by "evaluate". If you want extract a single
char just index the array, binary.
At the end I wrote
value=binary-'0'

and everything worked fine.


so you solved your problem. Why the post?


Well, I arrived there non wanting to go there.
what does a typical string look like and what does "evaluate" mean?
I mean. If I have the string "binary" containing, for example

"111011101"

I need to take every digit from "binary", and evaluate it, example

binary[0] is "1" that evaluate to 1 (the integer value rapresented by the
char)
binary[1] is 1
binary[2] is 1
binary[3] is 0

and so on...
 
N

Nick Keighley

try to leave more context in your post


Lampa said:
#include <string.h>
#include <stdlib.h>

int main (void)
{
char *binary;
char cypher;
int value;

binary = malloc (32);
scanf ("%s", binary);
n = strlen(binary);

for (i = 0; i < n; i++)
{
strcpy (cipher, binary ); expects two char*s value =
atoi(cipher);
/* ... */
}
}
}
I get a compilation error.

yup. strcpy(), as its name suggests, copies a string (an array of char)
you tried to copy a char.


Well, how do I copy a char, only writing
c=binary ?


exactly as you've written it?

surprise. atoi() takes char* and returns an int. binary isn't a char*.
Ok
What is the correct procedure to evaluate every single char in a string?

I don't know what you mean by "evaluate". If you want extract a single
char just index the array, binary.
At the end I wrote
value=binary-'0'

and everything worked fine.


so you solved your problem. Why the post?


Well, I arrived there non wanting to go there.


why did you not want to go there? I'm not trying to be difficult I
really don't understand what your problem is.

what does a typical string look like and what does "evaluate" mean?
I mean. If I have the string "binary" containing, for example

"111011101"

I need to take every digit from "binary", and evaluate it, example

binary[0] is "1" that evaluate to 1 (the integer value rapresented by the
char)
binary[1] is 1
binary[2] is 1
binary[3] is 0

and so on...

just print each character of the string. I'm not going to write the
program for you as I suspect it is homework. Post a compilable complete

program. Explain what the input is and what output you expect. I think
you're nearly there


--
Nick Keighley

There are only two ways to live your life.
One is as though nothing is a miracle.
The other is as though everything is a miracle.
Albert Einstein
 
W

Walter Roberson

I mean. If I have the string "binary" containing, for example

"111011101"

I need to take every digit from "binary", and evaluate it, example

binary[0] is "1" that evaluate to 1 (the integer value rapresented by the
char)

Your previous solution

value=binary-'0'

is fine.

There are other ways, such as

value = binary == '1';
or
value = (binary & 1) == ('1' & 1);

If you are doing a large number of tests, -lots- of data, then
it might be worth an approach such as

if ( '1' & 1 ) {
for ( /* loop conditions here */ ) {
value = binary & 1;
}
} else {
for ( /* loop conditions here */ ) {
value = !(binary & 1);
}
}

The else clause covers the uncommon (but not impossible) case that
'0' is represented by an odd number. This test is what is encapsulated
in my second form in the ('1' & 1) phrase; if you are doing a -lot-
of processing then you can effectively reduce the complexity of that
test from "is the result equal to the integer 1" to "is the result
non-zero"), which is more efficient on some architectures.
 

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

No members online now.

Forum statistics

Threads
474,169
Messages
2,570,919
Members
47,460
Latest member
eibafima

Latest Threads

Top