string question

T

Terry

If the string object contain like below:

string Mystring = " ";

What function should be used for checking the "Mystring" is no char inside?

Thanks

Terry
 
C

CBFalconer

Terry said:
If the string object contain like below:

string Mystring = " ";

What function should be used for checking the "Mystring" is no
char inside?

What does that mean? That string appears to contain whitespace
characters, either blanks or horizontal tabs. The individual
characters can be examined and possibly classified with such
standard functions as "isprint", "isdigit", "isalpha", etc.
Investigate things defined in ctype.h.

BTW string is not a legal identifier to use. You might have
declared that as"

char Mystring[] = " ";
or
char *Mystring = " ";

(which is not the same thing)

and then scan it with:

for (i = 0; i < strlen(Mystring); i++) {
if (iswhatever((unsigned char)Mystring)) {
/* do whatever */
}
}
 
N

Nick Landsberg

CBFalconer said:
Terry said:
If the string object contain like below:

string Mystring = " ";

What function should be used for checking the "Mystring" is no
char inside?


What does that mean? That string appears to contain whitespace
characters, either blanks or horizontal tabs. The individual
characters can be examined and possibly classified with such
standard functions as "isprint", "isdigit", "isalpha", etc.
Investigate things defined in ctype.h.

BTW string is not a legal identifier to use. You might have
declared that as"

char Mystring[] = " ";
or
char *Mystring = " ";

(which is not the same thing)

and then scan it with:

for (i = 0; i < strlen(Mystring); i++) {
if (iswhatever((unsigned char)Mystring)) {
/* do whatever */
}
}


better yet ...

limit = strlen(Mystring);
for( i = 0, i < limit; i++) {
/* etc. */
}


In another thread, there was a discussion about optimizing
certain library functions. While either of the above
constructs (yours or mine) would work, the use of strlen()
in the for() loop implies that "Mystring" will be walked
by strlen() for each invocation of the loop. If it's a big
string and there are a large number of such constructs, AND
the performance is unsatisfactory, then my small optimization
may help the matter.
 
A

Al Bowers

..................

BTW string is not a legal identifier to use. You might have
declared that as"

char Mystring[] = " ";
or
char *Mystring = " ";

What is wrong with:

typedef char string[156];

and then,

string Mystring = "Hello World!";
?
 
R

Richard Heathfield

Nick Landsberg wrote:

better yet ...

limit = strlen(Mystring);
for( i = 0, i < limit; i++) {
/* etc. */
}


In another thread, there was a discussion about optimizing
certain library functions. While either of the above
constructs (yours or mine) would work, the use of strlen()
in the for() loop implies that "Mystring" will be walked
by strlen() for each invocation of the loop. If it's a big
string and there are a large number of such constructs, AND
the performance is unsatisfactory, then my small optimization
may help the matter.

In fact, I had occasion to test the difference just recently, and found that
the performance increase was extremely significant for large strings. (In
fact, I think of it as a performance decrease, since my code already saves
the length...)
 
I

Irrwahn Grausewitz

What is wrong with:

typedef char string[156];

and then,

string Mystring = "Hello World!";
?

Identifiers that begin with str followed by a lowercase letter
are reserved for future library extensions (string.h).

typedef char String[156];

would be OK though, modulo style considerations. I'd never use
it anyway; YMMV (not that I think it does :)

Regards
 
P

pete

CBFalconer wrote:
BTW string is not a legal identifier to use.

Automatic variables which have the same names as standard
functions, don't cause a problem for the compiler.

#include <stdio.h>
#include <string.h>

int main(void)
{
int strcpy = 7;

printf("strcpy is %d\n", strcpy);
return 0;
}
 
J

Jens.Toerring

Automatic variables which have the same names as standard
functions, don't cause a problem for the compiler.
#include <stdio.h>
#include <string.h>
int main(void)
{
int strcpy = 7;
printf("strcpy is %d\n", strcpy);
return 0;
}

Are you sure?

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

int main( void )
{
int strcpy = 7;
const char *a = "Hello";
char b[ 6 ];

printf( "strcpy is %d\n", strcpy );
strcpy( b, a );
return EXIT_SUCCESS;
}

Regards, Jens
 
P

pete

Automatic variables which have the same names as standard
functions, don't cause a problem for the compiler.
#include <stdio.h>
#include <string.h>
int main(void)
{
int strcpy = 7;
printf("strcpy is %d\n", strcpy);
return 0;
}

Are you sure?

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

int main( void )
{
int strcpy = 7;
const char *a = "Hello";
char b[ 6 ];

printf( "strcpy is %d\n", strcpy );
strcpy( b, a );
return EXIT_SUCCESS;
}

My code was good,
but my way of describing the situation was inadequate.
Thank you.
It's best just to avoid reserved identifiers generally.
 
C

CBFalconer

Nick said:
CBFalconer said:
Terry wrote:
.... snip ...

and then scan it with:

for (i = 0; i < strlen(Mystring); i++) {
if (iswhatever((unsigned char)Mystring)) {
/* do whatever */
}
}


better yet ...

limit = strlen(Mystring);
for( i = 0, i < limit; i++) {
/* etc. */
}


I spent all of 5 seconds considering writing such an
'improvement', but decided it was most likely to confuse in view
of the apparent knowledge of the OP. I guess I should have added
a disclaimer to fend off the ravening wolves. :)
 
D

Dan Pop

In said:
Are you sure?

Dunno if he is, but I am.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main( void )
{
int strcpy = 7; ^^^^^^^^^^^^^^^
const char *a = "Hello";
char b[ 6 ];

printf( "strcpy is %d\n", strcpy );
strcpy( b, a );

Illegal usage of the strcpy identifier, whose current declaration is the
underlined one.

Each identifier must be used according to its declaration.
return EXIT_SUCCESS;
}

I'm not sure what you wanted to prove with your broken code, but Pete is
right:

- All identifiers with external linkage in any of the following
subclauses (including the future library directions) are always
reserved for use as identifiers with external linkage.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

It is perfectly OK to override a function declaration from a standard
header, at block scope, but the code must use the redeclared identifier
according to its current declaration, as long as it is in effect.

Here's a correct example:

#include <stdio.h>
#include <string.h>

int main(void)
{
const char *a = "Hello";
char b[6];
{
int strcpy = 7;
printf("strcpy is %d\n", strcpy);
}
strcpy(b, a);
return 0;
}

Of course, stylistically speaking, it's an atrocious thing to do, but the
program correctness is not affected, if done right.

Dan
 
P

Peter Pichler

Nick Landsberg said:
CBFalconer said:
for (i = 0; i < strlen(Mystring); i++) {
if (iswhatever((unsigned char)Mystring)) {
/* do whatever */
}
}


better yet ...

limit = strlen(Mystring);
for( i = 0, i < limit; i++) {
/* etc. */
}


Even better:

for (i = 0; Mystring; i++)
{
/* etc. */
}

Why going through the string twice (once in strlen(), once in your loop)
when once is enough?
 
M

Mark McIntyre

What is wrong with:
typedef char string[156];

all names "str" followed by a lowercase letter are reserved for future
library use. I guess its (I forget how namespaces work in C) that the
typedef'ed name doesn't clash, but why risk it?
 

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,137
Messages
2,570,797
Members
47,345
Latest member
tektheone

Latest Threads

Top