Thread: need help on strings and integer merging

  1. #1
    Unregistered
    Guest

    need help on strings and integer merging

    i am a beginner
    i have tried hours to figure out how to merge 2 strings and a integer into one string.
    if anyone can help me on that, i appreciate that.
    plz give me some codes too.
    thx

  2. #2
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Location
    Bangalore, India
    Posts
    345
    See the help on sprintf()
    Code:
    #include <stdio.h>
    
    main()
    {
            char str[255], *s1 = "The", *s2 = " Number is: ";
            int num = 666;
    
            sprintf(str, "%s%s%d", s1, s2, num);
            puts(str);
    }

  3. #3
    Unregistered
    Guest
    sorry i did not mention something
    after merging 2 strings and 1 number to 1 string, then assig it to another variable

    for example,

    a = "the", b ="number is" c = 999;
    d = a + b + c
    so d is a string

    thx

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    That's what sprintf() do. Instead of writing to the screen (like printf()) it writes to the string (str as in Shaik's example).
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    If you'll be displaying it without any further processing after this operation, printf can handle all that if you code in all the formating

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  2. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  3. Converting Strings To Integer In Linked List
    By rocky8015 in forum C Programming
    Replies: 3
    Last Post: 06-05-2005, 10:35 PM
  4. Need some help with integer strings
    By Guti14 in forum C++ Programming
    Replies: 4
    Last Post: 08-28-2003, 01:13 PM
  5. Strings to integer values
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 02-05-2002, 12:49 PM