473,657 Members | 2,513 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(3 2);
scanf("%s",bina ry);
n=strlen(binary );

If I write

for (i=0;i<n;i++)
{
strcpy(cipher,b inary[i]);
value=atoi(ciph er);
....
}

I get a compilation error.

If I write

cypher=atoi(bin ary[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 1656
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.t in.it>,
Lampa Dario <la**@dario.i t> 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
5822
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 characters (including &quot; and &apos;) I have java code using Xerces to select a node based on it's @OID attribute using an XPath like this:
6
8171
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. If String1.Length > 0 Then String2 = String1 where String1 = "" This statement also evaluates as true when String1 = "":
5
11448
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. Queston: How do you handle a string that contains both single & double quotes (i.e. 12'X7") Here's the function:
8
2683
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 returning null pointers.) It looks pretty watertight to me, but my version of lint complains about use of deallocated pointers, etc. Is this code completely safe on all input, or have I missed something? /* Header files included in the program...
5
1712
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 converted the linefeeds into characters. Is there a function to do that or do I need to write it myself?
5
7482
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 20.02.2006 etc... And I want to replace the /,.etc character with - (as 20-02-2006)
3
17164
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 is the fastest and the Regex.Matches.Count to be the slowest. I posted my results and source code to my web site. If you could take a look and maybe suggest an even faster method I'd like to hear from you. Also, if anyone can tell me why Regex is...
0
10576
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 characters or simply matching a single character) : ANSI-89 - Mainly used only by Jet / ACE SQL ANSI-92 - Mainly used by SQL Server and other grown-up products In the later versions of Access it is now possible to select ANSI-92 compatibility as an...
66
3129
by: dattts | last post by:
what is the difference between a single character and a string consisting only one character
0
8413
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8842
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8513
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8617
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6176
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4173
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2742
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1970
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1733
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.