473,473 Members | 1,844 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Switch based on a character in pointer

I am trying to rewrite a section of code within an existing program.

This is a section of code (simplified and non-working) showing the structure
that I am trying to achieve:

char *ParseString (char *instring)
{
char *ptr1=instring

if (instring == NULL)
return instring;

while (*ptr1) {
switch(*ptr1) {
case iscntrl(*ptr1)
*ptr1=' ';
case isspace(*ptr1)
*ptr1=' ';
case '+'
*ptr1=' ';
case '%'
*ptr1=' ';
}
ptr1++;
}
}

I am trying to first use the iscntrl and isspace functions to evaluate *ptr1.
From this I will overwrite *ptr1 with a character. Then I want to compare *ptr1
to a series of known characters via a switch statement, and then do some
processing based upon that, changing *ptr1.

How is the best way to achieve this ?

Please advise.

Mark.

--
Mark Hobley
393 Quinton Road West
QUINTON
Birmingham
B32 1QE

Telephone: (0121) 247 1596
International: 0044 121 247 1596

Email: markhobley at hotpop dot donottypethisbit com

http://markhobley.yi.org/

Dec 7 '05 #1
7 3359
Mark Hobley wrote:
I am trying to rewrite a section of code within an existing program.

This is a section of code (simplified and non-working) showing the structure
that I am trying to achieve:
simplified and "compilable" code would be appreciated.
char *ParseString (char *instring)
{
char *ptr1=instring

if (instring == NULL)
return instring;

while (*ptr1) {
switch(*ptr1) {
case iscntrl(*ptr1)
*ptr1=' ';
case isspace(*ptr1)
*ptr1=' ';
case '+'
*ptr1=' ';
case '%'
*ptr1=' ';
}
ptr1++;
}
}


For a switch statement, the "case has to be labeled by integer valued
constants or constant expression" ( K & R 3.4 ).

case iscntrl(*ptr1) and isspace(*ptr1) would not meet this requirement.
It would be great if you can post a code that could be "compiled"
(ofcourse with the errors that you are facing) rather than outlining
it.

Dec 7 '05 #2
On Wed, 07 Dec 2005 03:08:02 GMT, ma********@hotpop.deletethisbit.com
(Mark Hobley) wrote in comp.lang.c:
I am trying to rewrite a section of code within an existing program.

This is a section of code (simplified and non-working) showing the structure
that I am trying to achieve:

char *ParseString (char *instring)
{
char *ptr1=instring

if (instring == NULL)
return instring;

while (*ptr1) {
switch(*ptr1) {
case iscntrl(*ptr1)
You can't do this here. The value in a case statement must be a
compile-time integer constant expression. It cannot be the value of
any object, whether read through a pointer or not.
*ptr1=' ';
case isspace(*ptr1)
*ptr1=' ';
case '+'
*ptr1=' ';
case '%'
*ptr1=' ';
}
ptr1++;
}
}

I am trying to first use the iscntrl and isspace functions to evaluate *ptr1.
From this I will overwrite *ptr1 with a character. Then I want to compare *ptr1
to a series of known characters via a switch statement, and then do some
processing based upon that, changing *ptr1.

How is the best way to achieve this ?

Please advise.


When you can't use a switch/case control structure for reasons like
this, the alternative is if/else if.

while (*ptr1) {
if (iscntr(*ptr1)
{ /* do what you want */ }
else if (isspace(*ptr1)
{ /* do what you want */ }
else if (*ptr1 == '+')
{ /* whatever */ }
else if (*ptr1 == '%')
{ /* etcetera */ }
else
{ /* final else provides equivalent of default: in switch */ }
ptr1++;
}

You say your example is simplified, but if all of your actions are the
same (replace by the same character) there are various tweaks that
might or might not be more efficient on your implementation:

static const char repl [] = "+%"; /* and any others */

while (*ptr1)
{
if ( (isspace(*ptr1)
|| (isctrl(*ptr1))
|| (NULL != strchr(repl, *ptr1))
{ *ptr1 = ' '; }
++ptr1;
}

Warning the above is uncompiled and untested, so there's probably
something wrong with it.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Dec 7 '05 #3
Mark Hobley wrote:
while (*ptr1) {
switch(*ptr1) {
case iscntrl(*ptr1)
*ptr1=' ';
case isspace(*ptr1)
*ptr1=' ';
case '+'
*ptr1=' ';
case '%'
*ptr1=' ';
}
ptr1++;
}
while (*ptr1) {
if (iscntrl((unsigned char)*ptr1)) {
*ptr1 = ' ';
} else {
if (isspace((unsigned char)*ptr1)) {
*ptr1 = ' ';
} else {
switch (*ptr1) {
case '+':
*ptr1 = ' ';
break;
case '%':
*ptr1 = ' ';
break;
default:
break;
}
}
}
ptr1++;
}
I am trying to first use the iscntrl and isspace
functions to evaluate *ptr1.
From this I will overwrite *ptr1 with a character.
Then I want to compare *ptr1
to a series of known characters via a switch statement,
and then do some
processing based upon that, changing *ptr1.

How is the best way to achieve this ?


--
pete
Dec 7 '05 #4
ma********@hotpop.deletethisbit.com (Mark Hobley) writes:
I am trying to rewrite a section of code within an existing program.

This is a section of code (simplified and non-working) showing the structure
that I am trying to achieve:

char *ParseString (char *instring)
{
char *ptr1=instring

if (instring == NULL)
return instring;

while (*ptr1) {
switch(*ptr1) {
case iscntrl(*ptr1)
*ptr1=' ';
case isspace(*ptr1)
*ptr1=' ';
case '+'
*ptr1=' ';
case '%'
*ptr1=' ';
}
ptr1++;
}
}

I am trying to first use the iscntrl and isspace functions to
evaluate *ptr1. From this I will overwrite *ptr1 with a
character. Then I want to compare *ptr1 to a series of known
characters via a switch statement, and then do some processing based
upon that, changing *ptr1.


A switch statement can only compare an integer value to a series of
constants. You can have multiple case labels, but there's no
mechanism for specifying a range or calling a function.

I also note that you don't have a "break;" on each case, which means
each case will fall through to the next one. Assuming you don't want
it to fall through, your while loop can be replaced by:

while (*ptr1) {
if (iscntrl(*ptr1)) {
*ptr1=' ';
}
else if (isspace(*ptr1)) {
*ptr1=' ';
}
else if (*ptr1 == '+') {
*ptr1 = ' ';
}
else if (*ptr1 == '%') {
*ptr1 = ' ';
}
ptr1++;
}

You might also do something like this:

while (*ptr1) {
if (iscntrl(*ptr1) || isspace(*ptr1) ||
*ptr1 == '+' || *ptr1 == '%')
{
*ptr1 = ' ';
}
ptr1++;
}

or like this:

while (*ptr1) {
switch (*ptr1) {
case '+':
case '%':
*ptr1 = ' ';
break;
default:
if (iscntrl(*ptr1)) {
*ptr1=' ';
}
else if (isspace(*ptr1)) {
*ptr1=' ';
}
break;
}
ptr1++;
}

depending on what your full code looks like.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Dec 7 '05 #5
pete <pf*****@mindspring.com> wrote:
while (*ptr1) {
if (iscntrl((unsigned char)*ptr1)) {
*ptr1 = ' ';
} else {
if (isspace((unsigned char)*ptr1)) {
*ptr1 = ' ';
} else {
switch (*ptr1) {
case '+':
*ptr1 = ' ';
break;


Cheers Pete. That was exactly what I wanted to achieve. I had written
something similar, but in my version, I had overlooked the (unsigned char) bit,
This was causing a compile error, when the compiler reached the line after the
first case statement.

Mark.

--
Mark Hobley
393 Quinton Road West
QUINTON
Birmingham
B32 1QE

Telephone: (0121) 247 1596
International: 0044 121 247 1596

Email: markhobley at hotpop dot donottypethisbit com

http://markhobley.yi.org/

Dec 7 '05 #6

Mark Hobley wrote:
char *ParseString (char *instring)
{
char *ptr1=instring

if (instring == NULL)
return instring;

while (*ptr1) {
switch(*ptr1) {
case iscntrl(*ptr1)
*ptr1=' ';
case isspace(*ptr1)
*ptr1=' ';
case '+'
*ptr1=' ';
case '%'
*ptr1=' ';
}
ptr1++;
}
}


How about a for loop so the declaration, test and increment of prt1 are
together:

for( char *ptr1=instring; *ptr1; ptr1++ )
{
/* switch stuff */
}

Dec 7 '05 #7
pete <pf*****@mindspring.com> writes:
[...]
while (*ptr1) {
if (iscntrl((unsigned char)*ptr1)) {
*ptr1 = ' ';
} else {
if (isspace((unsigned char)*ptr1)) {
*ptr1 = ' ';
} else {
switch (*ptr1) {
case '+':
*ptr1 = ' ';
break;
case '%':
*ptr1 = ' ';
break;
default:
break;
}
}
}
ptr1++;
}


It's very common to write if-else chains on a single indentation
level:

while (*ptr1) {
if (iscntrl((unsigned char)*ptr1)) {
*ptr1 = ' ';
}
else if (isspace((unsigned char)*ptr1)) {
*ptr1 = ' ';
}
else {
switch (*ptr1) {
case '+':
*ptr1 = ' ';
break;
case '%':
*ptr1 = ' ';
break;
default:
break;
}
}
ptr1++;
}

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Dec 7 '05 #8

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

Similar topics

10
by: Myster Ious | last post by:
Polymorphism replaces switch statements, making the code more compact/readable/maintainable/OO whatever, fine! What I understand, that needs to be done at the programming level, is this: a...
13
by: webzila | last post by:
Hello, I have to write a program for an 8051 micro-controller using micro-C to monitor Switch 1 and if the switch in pushed the message "switch 1 pushed" should be displayed in the LCD. Also the...
6
by: bayxarea-usenet | last post by:
I am getting the following error during compiling: 'switch expression not integral' I am new to the switch command - here is a snip of my code where I have used this.. .. what is the problem...
13
by: Fei Liu | last post by:
Hi Group, I've got a problem I couldn't find a good solution. I am working with scientific data files in netCDF format. One of the properties of netCDF data is that the actual type of data is only...
7
by: cytec123187 | last post by:
Hello, I am working on an Adobe Acrobat file that uses javascript for calculations. I am trying to create a field that uses two other fields to determine a number value. I think this requires...
14
by: markww | last post by:
Hi, I want to use the vector container class to store pixel data. Currently I have some memory allocated using c++'s new operator. I allocate the memory differently based on if the pixel type is...
14
by: Bryan Parkoff | last post by:
Do you know that current C++ Compiler limits to 64KB segments in source code? It is good news that Microsoft Visual C++ 2005 has expanded to 4GB segments in source code. 4GB segment is ideal for...
6
by: asit | last post by:
please modify to get the correct output.(switch case is compulsory) #include <stdio.h> int main() { char ch; printf("Enter any character : "); ch=getch(); switch(ch)
2
by: Blip | last post by:
I try to update the buffer pointer in a switch statement like this: buf=(serialinput); switch (buf) that doesn't work correctly -- doesn't update the ptr I changed it to:...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...
0
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 ...
0
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...

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.