Thread: 2D array filling in spaces

  1. #1
    Registered User
    Join Date
    Mar 2019
    Posts
    1

    2D array filling in spaces

    insert
    Code:
    #include <stdio.h>
    
    
    int main(){
        char input[36];
        char i,j,count;
        char array[6][6];
        
         printf("Enter text:"); 
        scanf("%s", input);
        printf("\n%s", input);
        count=0; 
        printf("\n");
        printf("\n");
        
        
        for (i=0;i<6;i++){
            for (j=0;j<6;j++){
            array[i][j] = input[count];
            count++;
        }
        }
            
    
    
        for (i=0;i<6;i++){
                for (j=0;j<6;j++){
                    printf("%c", array[i][j]);
            }
            
            printf("\n");
            }
    
    	return 0; 
      
    }




    Im pretty new to C programming and i am trying to show a space when the user inputs one (e.g. if the user inputs a sentence) and then if the input does not fit the full space of the array it will input spaces to fill in the array, any help?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I suggest that you start with a simpler program: a program that will read a string of up to length 5 (hence an array of 6 char because of the null character), pad with spaces, then print the string within quotes (so you can tell that you correctly padded it with spaces).

    You won't be able to use %s with scanf for this as you want the user to be able to enter spaces. I suggest using fgets instead of scanf, but you would need to remove the trailing newline stored in the string, if there is space to store it. If you must use scanf, then consider using a scanset, e.g., %5[^\n].
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2D array filling
    By martynasj in forum C Programming
    Replies: 6
    Last Post: 11-16-2012, 04:14 PM
  2. Filling up a 2D (or 3D) array.
    By omnificient in forum C Programming
    Replies: 1
    Last Post: 01-20-2008, 01:22 PM
  3. Filling an array
    By nizbit in forum C Programming
    Replies: 3
    Last Post: 01-23-2005, 02:02 PM
  4. filling spaces in a string.
    By furiousferret in forum C++ Programming
    Replies: 3
    Last Post: 11-17-2004, 07:42 PM
  5. filling an array
    By Flex in forum C Programming
    Replies: 7
    Last Post: 02-28-2002, 03:11 PM

Tags for this Thread