help me with strings?

This is a discussion on help me with strings? within the C Programming forums, part of the General Programming Boards category; i still dont understand how i would go about saving a phrase to a variable..i always get errors like -"incompatiable ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    266

    help me with strings?

    i still dont understand how i would go about saving a phrase to a variable..i always get errors like
    -"incompatiable types in assignment"
    -"character constant to long"
    like this is wrong, how would i save "Hello World" to variable?
    Code:
    #include <stdio.h>
    int main ()
    {
    /* Wrong? */
    char mine[10];
    mine = 'Hello World';    /* i also tried using  -->  "  <-- still no   */  
    return 0;
    }
    Last edited by rodrigorules; 09-05-2005 at 07:02 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You use single quotation marks for single characters.
    You use double quotation marks for multiple characters (strings).
    You cannot assign a string to an array with the assignment operator.
    You must use functions designed for this action, or roll your own, and do it a character at a time. Such as strcpy
    You can assign them however, at the time you create the array.
    Code:
    char array[ BUFSIZ ] = "Hello World"; /* works because you're doing it when you declare the array */
    array = "doesn't work";
    
    strcpy( array, "does work" );
    I'd suggest reading up on the string manipulation functions. Doubtless your search engine of choice has numerous hits on the topic.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    If you want to initialise your char variable mine immediately, do so like this:

    Code:
        char mine[] = "Hello, World";
    Alternatively, you can declare the array, and copy the string in afterwards:

    Code:
        #include <string.h>
        ....
        char mine[15];
        strcpy(mine, "Hello, World");
    You cannot copy a string into an array by using the assignment operator as you tried. There are other ways of getting your text into an array, but that should get you started.

    ~/
    Last edited by kermit; 09-05-2005 at 07:17 PM.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    thanks!
    i didnt know u couldnt do it with '='


    (btw, when u program in C is it case sensitive?, like does
    "hello world" = "HeLLo WoRld" ? )

  5. #5
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Of course, "Hello, World" is actually 12 characters including the 0 NULL byte at the end, so 10 chars isn't appropriate, and space for it should be declared appropriately.

    >> "hello world" = "HeLLo WoRld"

    Nope those don't match. In addition, when you say in your code

    Code:
    char s[] = "Hello World!";
    Your s only contains a "pointer" to data elsewhere that says Hello World! These concepts will build up as you read through your tutorials. String comparison can be done with various functions in C. For case insensitive string comparison "hello world" == "HeLlO WoRlD" you can use stricmp(); it works something like this.

    Code:
    #include <string.h>
    #include <stdio.h>
    
    int main()
    {
    	char s[12];
    	strcpy(s, "Hello World!");
    
    
    	printf("%s", stricmp(s, "hElLo WoRlD!") ? 
    		"No they don't" : 
    		"Yes they match");
    
    
    	return 0;
    }
    
    // Output: Yes they match
    Also, in C, '=' is assignment i.e.

    Code:
    int someVariable = 3; // This makes someVariable 3
    and '==' is comparison, i.e.

    Code:
    someVariable == 3; // This checks if someVariable is 3

  6. #6
    Nonconformist Narf's Avatar
    Join Date
    Aug 2005
    Posts
    174
    Quote Originally Posted by Tonto
    In addition, when you say in your code

    Code:
    char s[] = "Hello World!";
    Your s only contains a "pointer" to data elsewhere that says Hello World!
    You're thinking of 'char *s', not 'char s[]'. The code you gave will generate an array, not a pointer. The array will be sized appropriately including the null character, and the contents of the string literal will be copied to the array as if you used strcpy(). On the other hand, if you used 'char *s' then you'd have a pointer to a string literal. There's a world of difference between these two declarations:
    Code:
    char s[] = "Hello World!";
    char *s = "Hello World!";
    For case insensitive string comparison "hello world" == "HeLlO WoRlD" you can use stricmp()
    If you have it. Case insensitive string comparison is a common extension, but it's not always called stricmp(). And there's no standard interface, so your example can only cover your compiler. Fortunately, the function is easy to write:
    Code:
    #include <ctype.h>
    
    int nocasecmp(const char *s1, const char *s2) {
      for (; toupper(*s1) == toupper(*s2); s1++, s2++) {
        if (*s1 == '\0') return 0;
      }
    
      return toupper(*s1) < toupper(*s2) ? -1 : +1;
    }
    Just because I don't care doesn't mean I don't understand.

  7. #7
    Registered User
    Join Date
    Sep 2005
    Posts
    8
    Code:
    sprintf(array,"Hello World\0");

  8. #8
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by ReXXuSS
    Code:
    sprintf(array,"Hello World\0");

    why use an expensive function like sprintf() to do a simple job that strcpy() is more efficient. And -- you don't have to terminate string literals with '\0' -- the compiler does that for you.
    Code:
    strcpy(array,"Hello World");

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 02:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 07:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  4. damn strings
    By jmzl666 in forum C Programming
    Replies: 10
    Last Post: 06-24-2002, 02:09 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21