Home Interview Questions and AnswersTechnical Interview Questions and AnswersC Programming C Language String Interview Questions and Answers For Freshers Part-3

string5. How can I pad a string to a known length?

Padding strings to a fixed length can be handy when you are printing fixed-length data such as tables or spreadsheets. You can easily perform this task using the printf() function. The following example program shows how to accomplish this task:
[C]
#include <stdio.h>

char *data[25] = {

“REGION”, “–Q1–“,    “–Q2–“,   “–Q3–“, ”  –Q4–“,

“North”, “10090.50”, “12200.10”, “26653.12”, “62634.32”,

“South”, “21662.37”, “95843.23”, “23788.23”, “48279.28”,

“East”, “23889.38”, “23789.05”, “89432.84”, “29874.48”,

“West”, “85933.82”, “74373.23”, “78457.23”, “28799.84” };

void main(void);

void main(void)

{

int x;

for (x=0; x<25; x++)

{

if ((x % 5) == 0 && (x != 0))

printf(“\n”);

printf(“%-10.10s”, data[x]);

}

}

printf(“%-10.10s”, data[x]);
[/C]
The “%-10.10s” argument tells the printf() function that you are printing a string and you want to force it to be 10 characters long. By default, the string is right-justified, but by including the minus sign (-) before the first 10, you tell the printf() function to left-justify your string. This action forces the printf() function to pad the string with spaces to make it 10 characters long. The result is a clean, formatted spreadsheet-like

output:

REGION      –Q1–   –Q2–     –Q3–    –Q4–

North      10090.50  12200.10  26653.12  62634.32

South      21662.37  95843.23  23788.23  48279.28

East       23889.38  23789.05  89432.84  29874.48

West       85933.82  74373.23  78457.23  28799.84

6. How can I copy just a portion of a string?

You can use the standard C library function strncpy() to copy one portion of a string into another string. Thestrncpy() function takes three arguments: the first argument is the destination string, the second argument is the source string, and the third argument is an integer representing the number of characters you want to copy from the source string to the destination string. For example, consider the following program, which uses thestrncpy() function to copy portions of one string to another:
[C]
#include <stdio.h>

#include <string.h>

void main(void);

void main(void)

{

char* source_str = “THIS IS THE SOURCE STRING”;

char dest_str1[40] = {0}, dest_str2[40] = {0};

/* Use strncpy() to copy only the first 11 characters. */

strncpy(dest_str1, source_str, 11);

printf(“How about that! dest_str1 is now: ‘%s’!!!\n”, dest_str1);

/* Now, use strncpy() to copy only the last 13 characters. */

strncpy(dest_str2, source_str + (strlen(source_str) – 13), 13);

printf(“Whoa! dest_str2 is now: ‘%s’!!!\n”, dest_str2);

}
[/C]
The first call to strncpy() in this example program copies the first 11 characters of the source string intodest_str1. This example is fairly straightforward, one you might use often. The second call is a bit more complicated and deserves some explanation. In the second argument to the strncpy() function call, the total length of the source_str string is calculated (using the strlen() function). Then, 13 (the number of characters you want to print) is subtracted from the total length of source_str. This gives the number of remaining characters in source_str. This number is then added to the address of source_str to give a pointer to an address in the source string that is 13 characters from the end of source_str.

Then, for the last argument, the number 13 is specified to denote that 13 characters are to be copied out of the string. The combination of these three arguments in the second call to strncpy() sets dest_str2 equal to the last 13 characters of source_str.

The example program prints the following output:

How about that! dest_str1 is now: ‘THIS IS THE’!!!

Whoa! dest_str2 is now: ‘SOURCE STRING’!!!

You may also like

Leave a Comment