How do I convert ints to chars, and print out an array in reverse?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sean Kernaghan
    New Member
    • Sep 2010
    • 5

    How do I convert ints to chars, and print out an array in reverse?

    Suppose I have an char array m, and I want to add an int value z into it, how do I convert z into a char so that I can put it into the array? Also, how could I print out the array in reverse? This is in C, NOT C++.
  • Meetee
    Recognized Expert Contributor
    • Dec 2006
    • 928

    #2
    Use sprintf function to insert number into array.

    To print reverse array start from n(array length) instead of 0 in print loop.(In this case n should be n+1 after adding number in array)

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      Please provide a short example so I understand what you want to do: initial contents of m[], value of z, final contents of m[]. In particular, please make it clear whether m[] contains character values (like '3') or small integer values (like 10). If it contains character values, then is it simply an array or is it a string? In a string, there are typically fewer meaningful characters than there are slots in the array; the end of the meaningful characters is indicated by a null character ('\0').

      Comment

      • Sean Kernaghan
        New Member
        • Sep 2010
        • 5

        #4
        The initial contents of m[] are blank but there are 100 slots, all blank for chars to be inserted in. I am trying to convert a number n to a base b (2-16), so z is a temporary number used to insert a number, or character into the array m[]. m could contain digits 0-9, and letters a-f lowercase.

        Comment

        • Sean Kernaghan
          New Member
          • Sep 2010
          • 5

          #5
          I am new to C so I don't know what the sprintf function is, could you please explain it to me?

          Comment

          • whodgson
            Contributor
            • Jan 2007
            • 542

            #6
            In C, a string is an array of characters which ends with a NUL character '\0' so the string s[16]represents the array below.(15 characters + NUL)
            So if char* s = "0123456789abcd e" that is s has a type pointer to a character then:
            Code:
            cout<<s<<endl; //prints 0123456789abcde
            There is an article on this forum which explains arrays in more detail.
            You could use a for loop to print out the array in reverse by starting at the [end-1]and printing each element until you reach s[0]

            Comment

            Working...