Char substring copy function
I have a working CHAR function, but would like some feedback on possibly making it better.
I haven't created the prototype yet, but expect it to take on 5 parameters.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
const int resultBuff = 20;
const int Buff = 20;
char ch[Buff] = "Joe Johnson";
char k[resultBuff] = ""; ;
int start = 5;
int length = 6;
int count = 0;
for (int i = start; i < start+length; i++)
{
k[i - start] = ch[i];
count++;
}
k[count] = 0;
cout << k << endl;
|
I have a working CHAR function, but would like some feedback on possibly making it better. |
This is a code snippet to extract a substring from a larger string. To make the code look better you can write like this :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
const int RESULT_STRING_SIZE = 20;
const int ORIGINAL_STRING_SIZE = 20;
char str_original[ORIGINAL_STRING_SIZE] = "Joe Johnson";
char str_result[RESULT_STRING_SIZE];
int str_start = 5;
int str_length = 6;
int string_len = strlen(str_original);
int i, count = 0;
for (i = str_start; i < str_start + str_length && i < string_len; i++, count++)
{
str_result[count] = str_original[i];
}
str_result[count] = 0;
cout << str_result << endl;
|
Topic archived. No new replies allowed.