I have a char array with a size of 17. I need to copy each block of 4 characters from the array into 4 separate char arrays. I'm not exactly how to go about this.

Recommended Answers

All 4 Replies

you can use strncpy() strncpy(a2, strings+4, 4); or you can just write a function that would copy 4 bytes from the starting address.
The latter is better, I think

I like the function better. I'm having a bit of a mind block about though. I know I have to iterate through arr[17]. But how do I stop at the 4th element, switch to a new array and read the next 4 chars?

I like the function better. I'm having a bit of a mind block about though. I know I have to iterate through arr[17]. But how do I stop at the 4th element, switch to a new array and read the next 4 chars?

Well I respect your zeal to write your own functions but what PRABAKAR said is absolutely right.You can use that standard function given in the library.(Standard functions are given so that programmers can code good programs without getting bogged down by certain stuff.)

If you want to code it your self do this:

//Declare four character arrays or an array as arr[4][5] ie four char //arrays to hold four characters and a '\0'

//Declare an array to take up your input string as str[17] or //something

//Take your input
i=0;
a=0;
while(i < strlen[str])
{
          for(j=0;j<4;)
                    arr[a][j++] = str[i++];
 
          arr[a][j]='\0';
          a++;          
}

The code is self explanatory. I have padded the arr array's 5th cell ie

arr[a][4] = '\0';

so that next time while printing you can just give

printf("%s",arr[a]);

Hope you can code as you wanted now.But I still recommend usage of standard functions when they are possible.

I wanted to say thanks for everyones help. Its been a little bit since a programmed c++ and a long time since c. I dare say they are one of the same but each with their own nuance and personality. I couldn't do another function because eventually this program will be converted to assembly. So the easier my c code, the easier my assembly. After I got my head in the game this is what I came up with:

#include "stdio.h"

int main()
{
	unsigned char array[17] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', '\0'};

	int i;
	char key1[5];
	char key2[5];
	char key3[5];
	char key4[5];
	
	for(i = 0; i < 4; i++)
	{
		key1[i] = array[i];
	}
	for(i = 4; i < 8; i++)
	{
		key2[i - 4] = array[i];
	}
	for(i = 8; i < 12; i++)
	{
		key3[i -8] = array[i];
	}
	for(i = 12; i < 17; i++)
	{
		key4[i - 12] = array[i];
	}

	key1[4] = '\0';
	key2[4] = '\0';
	key3[4] = '\0';
	key4[4] = '\0';

	for(i = 0; i < 4; i++)
	{
		printf("Key 1 = %c \n", key1[i]);
	}

	for(i = 0; i < 4; i++)
	{
		printf("Key 2 = %c \n", key2[i]);
	}
	
	for(i = 0; i < 4; i++)
	{
		printf("Key 3 = %c \n", key3[i]);
	}
	
	for(i = 0; i < 4; i++)
	{
		printf("Key 4 = %c \n", key4[i]);
	}
	
	return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.