In the following program everything works fine when I enter 5 or 8, but if I enter 4 or 7 the program crashes with a Segmentation fault error. I'm just trying to add zeros until the number of characters in string is evenly divisible by 3 and I'm doing so by padding it with zeros.
garrett@bedroom ~/Projects/TestArea $ ./addZeros 12345
12345
012345
garrett@bedroom ~/Projects/TestArea $ ./addZeros 1234
1234
Segmentation fault
#include<stdio.h>
#include<iostream>
#include<string.h>
int main(int argc, char *argv[])
{
char string[10] = "";
strcpy(string, argv[1]);
std::cout << string << std::endl;
int position = strlen(string);
while(strlen(string) % 3 != 0 || strlen(string) < 3)
{
for(int i = 0; i < strlen(string); i++)
{
string[position + 1] = string[position];
position--;
}
string[0] = '0';
}
std::cout << string << std::endl;
return 0;
}