473,395 Members | 1,631 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,395 software developers and data experts.

estracting single characters in a string

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[i]);
value=atoi(cipher);
....
}

I get a compilation error.

If I write

cypher=atoi(binary[i]);

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[i]-'0'

and everything worked fine.

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

--
Teaching OnLine
Corsi online di programmazione
Php, Asp, C, C++, Visual Basic, Delphi
Linux Shell Programming
------------------------------------
http://www.teachingonline.it

Nov 15 '05 #1
4 1626
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 [i]); 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]);

I get another compilation error .
surprise. atoi() takes char* and returns an int. binary[i] 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[i].
At the end I wrote
value=binary[i]-'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?
--
Nick Keighley

Nov 15 '05 #2
> #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 [i]); 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[i] ?
surprise. atoi() takes char* and returns an int. binary[i] 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[i].
At the end I wrote
value=binary[i]-'0'

and everything worked fine.


so you solved your problem. Why the post?


Well, I arrived there non wanting to go there.
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?

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...
--
Teaching OnLine
Corsi online di programmazione
Php, Asp, C, C++, Visual Basic, Delphi
Linux Shell Programming
------------------------------------
http://www.teachingonline.it

Nov 15 '05 #3
try to leave more context in your post
Lampa Dario wrote:
#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 [i]); 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[i] ?


exactly as you've written it?

surprise. atoi() takes char* and returns an int. binary[i] 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[i].
At the end I wrote
value=binary[i]-'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 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?

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

Nov 15 '05 #4
In article <m8*******************@news3.tin.it>,
Lampa Dario <la**@dario.it> wrote:
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[i]-'0'

is fine.

There are other ways, such as

value = binary[i] == '1';
or
value = (binary[i] & 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[i] = binary[i] & 1;
}
} else {
for ( /* loop conditions here */ ) {
value[i] = !(binary[i] & 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.
--
The rule of thumb for speed is:

1. If it doesn't work then speed doesn't matter. -- Christian Bau
Nov 15 '05 #5

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

Similar topics

3
by: Kevin | last post by:
I know this has probably been discussed many times before (I found answers when I searched yesterday), but I still can't get it to work... I have an attribute @OID that can contain any...
6
by: DLP22192 | last post by:
I have the following single-line if statement that is evaluating true even though it shouldn't. I have never seen this before and I am concerned that this can happen in other areas of my code. ...
5
by: Joel | last post by:
Hi, I incorporated a function in my code that whenever I use a string variable in an sql statement if the string contains a single quote it will encase it in double quotes else single quotes. ...
8
by: ais523 | last post by:
I use this function that I wrote for inputting strings. It's meant to return a pointer to mallocated memory holding one input string, or 0 on error. (Personally, I prefer to use 0 to NULL when...
5
by: Jayson Davis | last post by:
Say I have a string read from a configuration file. searchfor <tab> Needle\n\n Where I want to search for the word "Needle" with two linefeeds. Now when I read it from the file, it hasn't...
5
by: Niyazi | last post by:
Hi, Does anyone knows any good code for string manipulation similar to RegularExpresion? I might get a value as string in a different format. Example: 20/02/2006 or 20,02,2006 or ...
3
by: Dana King | last post by:
I'm looking for some other developers opinions, I'm trying to find the best way to count strings within a string using VB.net. I have tested five methods and have found the String.Replace method...
0
NeoPa
by: NeoPa | last post by:
ANSI-89 v ANSI-92 Before we get into all the various types of pattern matching that can be used, there are two ANSI standards used for the main types of wildcard matching (matching zero or more...
66
by: dattts | last post by:
what is the difference between a single character and a string consisting only one character
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.