Thread: how to copy the result of a printf() into an array ? alt, how to copy output to an ar

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    3

    how to copy the result of a printf() into an array ? alt, how to copy output to an ar

    i am trying to compare 2 strings of characters The users input containing 5 chars is compared to a table If the input is already be existent in the table the index of those chars in the table is printed Quest: how to copy the result of a printf() into an array ? The last printf() gives a sequence of numbers and I am trying to save that sequence to another array for further operation ! I have not been able to do that so far even with “ tmp[]=i ;”

    Code:
    
    #include <stdio.h>
                #include <string.h>
                #define N 30
                #define n 100
                int main (void)
                {
                    int i,z;
                    int total_chars = 0;
                    char seq[N],c;
                    int tmp[13];
    
                    char Table[] = {'z','c','C','d','D','e','f','F','g','G','a','A','h','c'
                                    ,'C','d','D','e','f','F','g','G','a','A','h','c'
                                   };
    
                    for ( i=0 ; i<13; i++)
                    {
                        c=getchar();
                        seq[i]=c;
                        printf( "your input %c\n" , seq[i]);
    
                        if(seq[i] == '0')
                        {
                            total_chars = i;
                            break;
                        }
    
                    }
    
    
                    for(z=0 ; z<13 ; z++ )
                    {
                        for (i=0; i<13; i++)
                        {
    
                            if (seq[z]==Table[i])
                                // seq[z]=i;
    
                                printf("\n%d",i);   // here i get a sequence of numbers the question is how can i copy this  sequence to an array and the print the array out ?
    
    
                        }
    
                    }
    
    
    
    
    
                    return 0;
                }

  2. #2
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Look into sprintf() and snprintf()

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    tmp[] = i is fairly close, but you have to include an array position inside the brackets (ie you can do tmp[k] = i). This means you're going to have to (a) make sure tmp exists and has enough space and (b) keep track of the position you currently are writing to (ie create a variable k and make sure it has the right value).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. a simple input to output c copy program
    By Nishant Dwivedi in forum C Programming
    Replies: 7
    Last Post: 12-13-2012, 07:49 AM
  2. Can i copy the output of system() to an array?
    By xenanovich in forum Linux Programming
    Replies: 9
    Last Post: 05-26-2010, 12:25 AM
  3. How to copy value from MySQL result to string pointer?
    By fekkesh in forum C Programming
    Replies: 2
    Last Post: 03-12-2009, 08:52 AM
  4. How do you copy one array in C to another?
    By cus in forum C Programming
    Replies: 26
    Last Post: 01-03-2009, 08:21 AM
  5. copy application-output into a txt-file
    By xB4nk3R in forum C Programming
    Replies: 3
    Last Post: 05-02-2003, 02:10 PM

Tags for this Thread