I need to read some numbers from a file and generate all the numbers which can be formed by removing 1 digit from the start and one from the end ( for each one of my numbers read ) and print them into a new file.I know how to read and print in files so my question is more about the way I should think when solving this problem. Here is an example for this problem:
for 3457 the output should be:
457
345
34
45
57
3
4
5
7
my initial thought was to read the numbers as an array of strings using a double pointer ( assuming I know how many numbers I should read so I can dynamically allocate memory to it ) and then to work on them.
After that I though of a way to generate the numbers that are obtained by removing 1 digit from the start and one from that end ( but not simultaneously ) for that I used a for loop:
for ( i = 1, j = (strlen(p[0]) - 2); i < strlen(p[0]) - 2, j >=0; ++i, --j ) //p[0] is my first number read from the file
{
printf("\n%s", p[0] + i); //this will print the numbers 457, 57, 7
char temp[10];
strncpy(temp, p[0], j);//I copy into a new temporary string to print the numbers 345, 34, 3
temp[j] = '\0';
printf("%s", temp);
printf("\n%s", temp + i);//this will print the numbers 45,4
}
But I have no idea on how to proceed with the rest of the numbers and i simply wasn't able to find or to think of an algorithm which will print them in that order, so I would really appreciate some help on how to solve this problem