Characters of String 
Author Message
 Characters of String

Greetings C Group,

This question is probably too easy for the FAQ because I didn't find any
info on it. I checked sections 6. Arrays and Pointers, and 8. Characters and
Strings.

char MyString[100];

/* want to find a space inside string */

int len = strlen(MyString);
int loop1, loop2;

for (loop1=0; loop1 < len; loop1++){
     if ( strcmp(MyString[loop1], " ") == 0) {     /*check for spaces in
string*/
        /* etc */
    }

Quote:
}

This obviously doesn't work, and the FAQ made a distinction between
characters and strings. strcmp is a string function. Are there any character
functions then?

Also, say I have a string

char AnotherString[] = "This is my string";

How can I "star" out parts --- for example I tried:

/* star out the word "my" in the string */

strcpy(AnotherString[8], "*");    /*star out m*/
strcpy(AnotherString[9], "*");    /*star out y*/

again, strcpy is a string function and doesn't work with characters. So how
can I solve this problem

Thank you for reading my question,
William



Wed, 30 Aug 2000 03:00:00 GMT  
 Characters of String

Hey, try passing the ADDRESS of the character you are trying to use in strcmp()
or strcpy().   Like, instead of array[x], use &array[x].

-SC



Thu, 31 Aug 2000 03:00:00 GMT  
 Characters of String

Hey William,

You should check the compiler warnings and try to understand them.  And the
replacement of individual characters in your string using strcpy() won't work
as you expect it to.  Use memcpy() instead.

-SC



Thu, 31 Aug 2000 03:00:00 GMT  
 Characters of String

Quote:

>This question is probably too easy for the FAQ because I didn't
>find any info on it. I checked sections 6. Arrays and Pointers, and
>8. Characters and Strings.

It's great that you checked the C-faq!  *clap* *clap*  I'm sorry you
did not find an answer to your question.  However, you should consider
reading the C-faq in its entirety, since it offers a lot of advice and
solutions to common C programming pitfalls and problems.

Quote:
>char MyString[100];

>/* want to find a space inside string */

>int len = strlen(MyString);
>int loop1, loop2;

>for (loop1=0; loop1 < len; loop1++){
>     if ( strcmp(MyString[loop1], " ") == 0) {     /*check for spaces in
>string*/
>        /* etc */
>    }
>}

The intended purpose is clear, but realize that MyString[loop1] is a
character, which " " is a string.  Your compiler should have
complained about the call to strcmp().  You *did* #include <string.h>,
right?

If you had used the space character ' ', then you could make your loop
work with this change:

     if ( MyString[loop1] == ' ') { /* check for spaces in string */

That is, you can compare character values since.  This is because in
C, char's are an integral type.

Quote:
>This obviously doesn't work, and the FAQ made a distinction between
>characters and strings. strcmp is a string function. Are there any
>character functions then?

There is a function call strchr().  An example of its use can be seen in
question 7.1 of the C-faq.  But, depending on what you do in the /* etc */
part of your loop, you may or may not want to use it.

Quote:
>Also, say I have a string

>char AnotherString[] = "This is my string";

>How can I "star" out parts --- for example I tried:

>/* star out the word "my" in the string */

>strcpy(AnotherString[8], "*");    /*star out m*/
>strcpy(AnotherString[9], "*");    /*star out y*/

Once again, AnothterString[8] represents a char object.  Your compiler
should have complained when you attempted this function call.  If it
did not, then you probably forgot to #include <string.h> at the top of
your source file.

Since AnotherString[8] is a char object, you can assign a char object
to it.  In this case, you want to use the '*' character.

  AnotherString[8] = '*';

Quote:
>again, strcpy is a string function and doesn't work with characters.
>So how can I solve this problem

You could have also tried to use strncpy to solve this problem, but
you would have to know how to pass in a pointer to char.

It seems that you are struggling with some fundamental C concepts.  In
C, an array is perhaps the most fundamental aggregate type.  It is
vital to understand how to manipulate them and each of their member
elements.  Consider reviewing a C tutorial on arrays.

Also, you did not seem to know what library functions are available to
you.  You should read through all the library functions available to
your from your C language reference so as to be aware of what problems
C can solve for you.  It will save you time programming in the long
run.  This is the 7th commandment to C programmers.  If you want to
read the other commandments, browse:

  http://www.plethora.net/~seebs/c/10com.html

--

http://www.cs.wustl.edu/~jxh/        Washington University in Saint Louis

Quote:
>>>>>>>>>>>>> I use *SpamBeGone* <URL:http://www.internz.com/SpamBeGone/>



Thu, 31 Aug 2000 03:00:00 GMT  
 Characters of String


Quote:
>Hey, try passing the ADDRESS of the character you are trying to use in
>strcmp() or strcpy().  Like, instead of array[x], use &array[x].

This solution is inadequate for solving the problems the person had.

It might work if you had mentioned he may need to use strncmp() and
strncpy().  These versions take an extra parameter though.  Look in
your C language reference documentation about what the arguments mean.

--

http://www.cs.wustl.edu/~jxh/        Washington University in Saint Louis

Quote:
>>>>>>>>>>>>> I use *SpamBeGone* <URL:http://www.internz.com/SpamBeGone/>



Thu, 31 Aug 2000 03:00:00 GMT  
 Characters of String

Quote:

> Greetings C Group,

> This question is probably too easy for the FAQ because I didn't find any
> info on it. I checked sections 6. Arrays and Pointers, and 8. Characters and
> Strings.

> char MyString[100];

> /* want to find a space inside string */

> int len = strlen(MyString);
> int loop1, loop2;

> for (loop1=0; loop1 < len; loop1++){
>      if ( strcmp(MyString[loop1], " ") == 0) {     /*check for spaces in
> string*/
>         /* etc */
>     }
> }

> This obviously doesn't work, and the FAQ made a distinction between
> characters and strings. strcmp is a string function. Are there any character
> functions then?

  A whole bunch of them (defined in <ctype.h>):
   int isspace(char);
   int isalpha(char);
   int isalnum(char);
   int isupper(char);
   int islower(char);
   int isdigit(char);
   int isxdigit(char);
   int iscntrl(char);
   int ispunct(char);
   int isprint(char);
   int isgraph(char);
There are also their close relatives in <wctype.h>.

Quote:
> Also, say I have a string

> char AnotherString[] = "This is my string";

> How can I "star" out parts --- for example I tried:

> /* star out the word "my" in the string */

> strcpy(AnotherString[8], "*");    /*star out m*/
> strcpy(AnotherString[9], "*");    /*star out y*/

> again, strcpy is a string function and doesn't work with characters. So how
> can I solve this problem

  AnotherString[8] = '*';
  AnotherString[9] = '*';

Have a look also at memcpy() and memcmp() functions.

        Regards,
                Alex Krol



Thu, 31 Aug 2000 03:00:00 GMT  
 Characters of String

Quote:

> Greetings C Group,

> This question is probably too easy for the FAQ because I didn't find any
> info on it. I checked sections 6. Arrays and Pointers, and 8. Characters and
> Strings.

> char MyString[100];

> /* want to find a space inside string */

> int len = strlen(MyString);
> int loop1, loop2;

> for (loop1=0; loop1 < len; loop1++){
>      if ( strcmp(MyString[loop1], " ") == 0) {     /*check for spaces in
> string*/
>         /* etc */
>     }
> }

> This obviously doesn't work, and the FAQ made a distinction between

  Something like this will work:

  char *str;

  str=MyString;

  for(loop1=0;loop1<len;loop1++){
    if(*(str+loop)==' ' ){

      /* do something here */
    }
  }

Quote:
> characters and strings. strcmp is a string function. Are there any character
> functions then?
> Also, say I have a string

> char AnotherString[] = "This is my string";

> How can I "star" out parts --- for example I tried:

> /* star out the word "my" in the string */

> strcpy(AnotherString[8], "*");    /*star out m*/
> strcpy(AnotherString[9], "*");    /*star out y*/

> again, strcpy is a string function and doesn't work with characters. So how
> can I solve this problem

  char *targ="my";  /* static here but, obviously, can be loaded at runtime */
  char *loc;
  int sub,pos;

  sub=strlen(targ);

  loc=strstr(MyString,targ);
  if(loc!=NULL){
    for(pos=0;pos<sub;pos++){
      *(loc+pos)='*';
    }
  }

Steve Rountree



Thu, 31 Aug 2000 03:00:00 GMT  
 Characters of String

...

Quote:
>> This obviously doesn't work, and the FAQ made a distinction between
>> characters and strings. strcmp is a string function. Are there any character
>> functions then?

>  A whole bunch of them (defined in <ctype.h>):
>   int isspace(char);
>   int isalpha(char);
>   int isalnum(char);
>   int isupper(char);
>   int islower(char);
>   int isdigit(char);
>   int isxdigit(char);
>   int iscntrl(char);
>   int ispunct(char);
>   int isprint(char);
>   int isgraph(char);

These all take int arguments and require that the value you pass be
representable as an unsigned char or else be the value of the EOF macro.

--
-----------------------------------------


-----------------------------------------



Fri, 01 Sep 2000 03:00:00 GMT  
 Characters of String

: Greetings C Group,

: This question is probably too easy for the FAQ because I didn't find any
: info on it. I checked sections 6. Arrays and Pointers, and 8. Characters and
: Strings.

: char MyString[100];

: /* want to find a space inside string */

: int len = strlen(MyString);
: int loop1, loop2;

/* ### intialise MyString here:
   strcpy(MyString, "Fresh fish!");
 */
: for (loop1=0; loop1 < len; loop1++){
:      if ( strcmp(MyString[loop1], " ") == 0) {
:      /*check for spaces in string*/
/* strcmp() takes two string pointer arguments, and MyString[loop1]
 * is a character.  Further, strcmp() checks two strings terminated
 * by null characters ('\0'), so " " will never compare equal to any
 * part of "Fresh fish!".  Use something like:
   if (MyString[loop1] == ' ')
 * and read up carefully on strings and characters.
 */
:         /* etc */
:     }
: }

: This obviously doesn't work, and the FAQ made a distinction between
: characters and strings. strcmp is a string function. Are there any character
: functions then?

Just straight equality will do.

: Also, say I have a string

: char AnotherString[] = "This is my string";

: How can I "star" out parts --- for example I tried:

: /* star out the word "my" in the string */

: strcpy(AnotherString[8], "*");    /*star out m*/
: strcpy(AnotherString[9], "*");    /*star out y*/

: again, strcpy is a string function and doesn't work with characters. So how
: can I solve this problem

Use a loop, as you did above, if you want to star out single characters.
If you want to star out strings it's more difficult; you really have to
use pointer access:
  #include <string.h>
  ...
  char *cp, *bp = "is";
  char array[] = "This is a crisis!";

  for (cp = array ; *cp ; cp++)
    if (strncmp(cp, bp, strlen(bp)) == 0)
        for (j = 0 ; j < strlen(bp) ; j++)
            *cp++ = '*';

That's obscure code to someone new to C, but I can't think of a simpler
way to put it.  For a fixed string, you can replace bp by "my" everywhere,
and strlen(bp) by 2, but it's still not simple.  It will also stamp on
the marked string even if it's within a word, as in the example above.

Will



Fri, 01 Sep 2000 03:00:00 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. HowDoI: Remove NULL characters from string

2. Filter out unwanted characters from string

3. characters and strings

4. replace one character in string

5. null character in strings... strange behavior

6. Characters in strings

7. replacing characters in string

8. Removing Special Characters From Strings

9. Multi-byte characters and string matching

10. type convert from character to string

11. characters and strings

12. returning index # of first character in string ..etc

 

 
Powered by phpBB® Forum Software