Thread: How to shift an array and add a char to the middle?

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    5

    How to shift an array and add a char to the middle?

    I have searched the forum and couldn't really find anything. Which seems strange because this seems like it would be a common topic.

    I need to shift the last item of my array and add a space/spaces to the left of that item.

    Basically I am justifying an array with random numbers of spaces depending on the size of the array/line.

    I am having trouble with:

    1.) Figuring out how to find the location of the last element in the array.

    2.) Understanding how to add an element to the middle of an array that is different sizes depending on each loop that sets the size of the array.

    Thanks,
    Tanner

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by vertigodown
    I need to shift the last item of my array and add a space/spaces to the left of that item.
    So, you have an array of char? Is this a null terminated string?

    Quote Originally Posted by vertigodown
    Basically I am justifying an array with random numbers of spaces depending on the size of the array/line.
    What exactly do you mean by "justifying an array with random numbers of spaces"? It sounds as if you have an array of arrays of char (or some other type), and then you are trying to justify the print out, rather than justify the array itself, whatever that means.
    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

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    5
    Yes I was trying to keep it brief but I suppose I will just add the code to avoid confusion.

    I believe it is a null terminated string of type char.

    And yes I am trying to justify the output by adding spaces to the char array.

    Code:
    
    #include <stdio.h>
    #include <string.h>
    #define MAX_LINE_LEN 62
    
    
    
    
    int output (char wbuf[]);
    int space_check(int line_len);
    int get_text(void);
    
    
    int lbuf_len = 0;
    
    
    char lbuf[62], *p = lbuf;
    
    
    
    
    	
    
    
    	FILE *gettyText;
    	FILE *csis2;
    
    
    int main(void){
    
    
    	p[0] = '\0';
    
    
    	fopen_s(&gettyText, "getty.txt", "r");
    	fopen_s(&csis2, "csis2.txt", "w");
    
    
    	get_text();
    	
    	getchar();
    	return 0;
    }
    
    
    int get_text(void)
    {
    	int n = 0;
    
    
    	extern char lbuf[62], *p;
    	char wbuf[60];
    	
    	while (!feof(gettyText))
    	{
    		fscanf(gettyText, "%s", wbuf);
    		output(wbuf, n);
    		n++;
    	}
    
    
    	if(feof(gettyText)){
    			puts(lbuf);
    	}
    
    
    		return 0;
    		
    	getchar();
    	return 0;
    
    
    }
    
    
    int output(char wbuf[], int n)
    {
    	int line_len, spaces_needed;
    	extern int lbuf_len;
    	int wordLength;
    	extern char lbuf[62], *p;
    	char i[2], *j;
    
    
    	spaces_needed = 0;
    	
    	
    	
    	wordLength = strlen(wbuf);			// determines the size of the word
    	// printf("###%d###\n", wordLength);	
    	lbuf_len += wordLength; 
    	// printf("$$$$%d$$$\n", lbuf_len);
    
    
    	if(space_check(lbuf_len) > wordLength ){
    
    
    		// printf("%s", wbuf);
    		strcat(lbuf,wbuf);
    		strcpy (i, " ");
    		strcat(lbuf, i);
    		lbuf_len++;
    		//getchar();
    		// puts(lbuf);
    			}
    
    
    	if (space_check(lbuf_len) < wordLength)
    	{
    		spaces_needed = (strlen(lbuf) - 62);
    		printf("%d", line_len);
    		getchar();
    		fmt(spaces_needed);
    		
    		puts(lbuf);
    		lbuf_len = 0;
    		p[0] = '\0';
    		 //printf("Cleared space_remaining");
    		strcat(lbuf,wbuf);
    		strcpy (i, " ");
    		strcat(lbuf, i);
    		lbuf_len = strlen(wbuf);
    		//printf("Check new value of lbuf after it is cleared: %d", lbuf_len);
    			}
    
    
    	return 0;
    
    
    }
    	
    int space_check(int lbuf_len){
    
    
    	int space_remaining;
    
    
    	space_remaining = MAX_LINE_LEN - lbuf_len;
    
    
    	//printf("%d Is the space remaining:  ", space_remaining);
    
    
    	return space_remaining;
    
    
    }
    /*
    int fmt(int spaces_needed){
    	int z = 0;
    
    
    	while (spaces_needed < MAX_LINE_LEN, spaces_needed++){		// Function to insert spaces into lbuf array
    
    
    
    
    	}
    
    
    }
    */

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Well, not sure if you need to alter the array for that (you can just print out the spaces you want and then the array) but if you really need to I suggest the following strategy:
    1. malloc enough memory for your altered string (spaces + original string)

    2. Add however many spaces you like to it and null terminate it.

    3. Strcat the original string onto it.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to shift the bits of an array?
    By manasij7479 in forum C Programming
    Replies: 6
    Last Post: 08-13-2011, 07:42 PM
  2. Shift right for an array .
    By amir1986 in forum C Programming
    Replies: 4
    Last Post: 01-22-2011, 06:56 AM
  3. Initializing 2D array in Middle of program
    By subhadeepgayen in forum C Programming
    Replies: 3
    Last Post: 09-06-2010, 06:04 AM
  4. How to shift elements of an array?
    By zeebo17 in forum C Programming
    Replies: 25
    Last Post: 06-09-2010, 07:44 PM
  5. How to find the middle element of a bulk array?
    By void_mehboob in forum C Programming
    Replies: 4
    Last Post: 04-19-2009, 11:37 PM