string length

This is a discussion on string length within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <string.h> #include <stdlib.h> int main() { char word[10]; /* Declare a string */ int i; /* ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    17

    string length

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    
    int main()
    {
    
    char word[10];   /* Declare a string */
    int i;                    /* Declare our variables */
    int sl; 
    int s; 
    int total;
    
    puts("Enter your word: ");      /* Get the word from the user */
    gets(word);
    
    printf("Enter your number:");    /* Get the shift value */
    scanf("%d",&i);
    
    sl=strlen(word); /* Get the string length */
    s=sizeof(word);      /* Get the sizeof the string  */
    total=strncat(word, i,8);
                       
    printf("\nThe lenght of the string is %d",sl);   
    printf("\nThe size is %d\n",s);
    
    printf("Your word with the number added is: ");    /* Print out the result */
    printf("%s %d\n\n",word, i);
    
    printf("\nThe new lenght of the string is %s", total);   
    printf("\nThe size is %d\n",s);
       
    }
    im cracking up with this one, im at this since 11 o clock this morning, i want the number of characters in the new string, "word+i=length"
    thanks in advance

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Code:
    printf("\nThe new lenght of the string is %s", total);
    What is type of total. what should be format specifier for that type?
    Btw, gets() is dangerous.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Well, for one thing you cannot concactenate an integer onto the end of a string they are incompatible types and your compiler should be telling you so...

  4. #4
    Registered User
    Join Date
    Jan 2011
    Posts
    17
    you cannot concactenate an integer onto the end of a string
    The first program i wrote on this was adding "word" to "i", like "Jaws" and "2" and giving Jaws2, i can get it to count the characters in the word "Jaws" i.e 4, but i cant get it to count the characters in "Jaws2"

  5. #5
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,650
    Code:
    s=sizeof(word);      /* Get the sizeof the string  */
    That doesn't do what you think.

    Why not tell us exactly what your input/output is?

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,465
    Code:
    total_len = strlen(word1) + strlen(word2);
    // concatenate the strings into new_word
    printf("Your new word is %s, it has %d chars", new_word, total_len);

  7. #7
    Registered User
    Join Date
    Jan 2011
    Posts
    17
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main()
    {
    char word[10];   /* Declare a string */
    int i;                    /* Declare our variables */
    int sl; 
    int s; 
    
    puts("Enter your word: ");      /* Get the word from the user */
    gets(word);
    
    printf("Enter your number:");    /* Get the shift value */
    scanf("%d",&i);
    
    sl=strlen(word); /* Get the string length */
    s=sizeof(word);      /* Get the sizeof the string  */
                       
    printf("\nThe lenght of the string is %d",sl);   
    printf("\nThe size is %d\n",s);
    
    printf("Your word with the number added is: \n");    /* Print out the result */
    printf("%s %d\n\n",word, i);
    }
    Basically this one gives you

    Code:
    Enter your word:
    Jaws
    Enter your number:
    2
    The length of the string is 4
    The size is 10
    Your word with the number added is: Jaws 2
    the size of is always 10 as declared at the start of the code,
    i want to count the extra characters (space+2) in a separate piece of code to go under neath this

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,465
    You will have to use something like sprintf to put the space+2 into a buffer, then you can use strlen on that to get the length of the extra stuff.

  9. #9
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,465
    Or, printf will return the number of characters printed. So you could do a little math with that return value and the length of the original string they entered.

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by larrydeloafer View Post
    The first program i wrote on this was adding "word" to "i", like "Jaws" and "2" and giving Jaws2, i can get it to count the characters in the word "Jaws" i.e 4, but i cant get it to count the characters in "Jaws2"
    Sorry nope... you can add a character (1 byte ascii) but not an integer (4 bytes binary) they are incompatible.

    Your second code fragment where you end up with "The word with the number add is ..." doesn't actually add the integer to any string... it simply puts it on the screen behind the string... It's a display trick, that's all.
    Last edited by CommonTater; 03-02-2011 at 02:11 PM.

  11. #11
    Registered User
    Join Date
    Jan 2011
    Posts
    17
    yes tater thats exactly what i want it to do, obviously you cant add a word to a number, what i want my code to do is count how many characters are in the string after i have put the number to the end of it

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by larrydeloafer View Post
    yes tater thats exactly what i want it to do, obviously you cant add a word to a number, what i want my code to do is count how many characters are in the string after i have put the number to the end of it
    Look up snprintf() in your library docs.

    Code:
    int x = 10;
    char str[] = "This is a test";
    
    char result[21] = {0};
    
    snprintf(result,20,"%s %d",str,x);
    
    printf ("%s    is %d characters long", result,strlen(result));

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polymorphism and generic lists
    By Shibby3 in forum C# Programming
    Replies: 9
    Last Post: 07-26-2010, 05:27 AM
  2. C++ FTP class won't work
    By lord mazdak in forum C++ Programming
    Replies: 8
    Last Post: 12-18-2005, 06:57 AM
  3. Weird modification to string length
    By ChwanRen in forum C Programming
    Replies: 0
    Last Post: 08-17-2003, 10:45 AM
  4. Basic C Programming Help Needed
    By Smurphygirlnz in forum C Programming
    Replies: 8
    Last Post: 09-26-2002, 07:12 PM
  5. string handling
    By lessrain in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 07:36 PM

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