C

Topic: Strings

How do you print only part of a string?

The following program shows how to print only part of a string using the printf() function:
#include <stdio.h>
#include <string.h>
void main(void);
void main(void){ 
char* source_str = "THIS IS THE SOURCE STRING";  /* Use printf() to print the first 11 characters of source_str. */  
printf("First 11 characters: '%11.11s'n", source_str);  /* Use printf() to print only the  last 13 characters of source_str. */  
printf("Last 13 characters: '%13.13s'n",    source_str + (strlen(source_str) - 13));}

This example program produces the following output:
First 11 characters: 'THIS IS THE'Last 13 characters: 'SOURCE STRING'The first call to printf() uses the argument "%11.11s" to force the printf() function to make the output exactly 11 characters long. Because the source string is longer than 11 characters, it is truncated, and only the first 11 characters are printed. The second call to printf() is a bit more tricky. 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. By using the argument "%13.13s", the program forces the output to be exactly 13 characters long, and thus the last 13 characters of the string are printed.

Browse random answers:

How to compare two strings in c ?
What is the difference between System.String and System.StringBuilder classes?
How would you implement a sbstr()function that extract a sub string from a givn string?
How can i print a?%? character in a print format string? 
Explain one method to process an entire string as one unit?
Can we use string in switch statement?
What is the difference between strings and character arrays ?
How do you print only part of a string?
How to type a string without using printf function?
How can I convert a number to a string?
How can I convert a string to a number?
What is the difference between a string copy (strcpy) and a memory copy (memcpy)? When should each be used?
What is the difference between a string and an array? 
How to get string in files and print the string in the reverse order?
If the two strings are identical, then strcmp() function returns?
What is the difference between the functions strdup and strcpy in C?
What do you mean by String objects are immutable?
How do you create empty strings in C?
Write a scanf statement which can store one line of string which includes white space?
What will be output when you will execute following c code?
#include&lt;stdio.h&gt;
void main(){ 
char arr[7]=&quot;Network&quot;; 
printf(&quot;%s&quot;,arr);
}
Which of the following function sets first n characters of a string to a given character?
What will be the output of the program ?

#include&lt;stdio.h&gt;#include&lt;string.h&gt;

int main(){ char str1[20] = &quot;Hello&quot;, str2[20] = &quot; World&quot;; printf(&quot;%sn&quot;, strcpy(str2, strcat(str1, str2))); 
 
return 0;
}
Without using any semicolon (;) in program write a c program which output is: HELLO WORLD?
How can I remove the trailing spaces from a string?
How can I right-justify a string?
How can you tell whether two strings are the same?
How do you print only part of a string?
Is there a way to switch on strings? 
How would you use the functions fseek(), freed(), fwrite() and ftell()?
Difference between strdup and strcpy?
What are strings and how can they be represented in c language?
How can you tell whether two strings are the same?
Difference between strdup and strcpy?
How do I use c-strings to write a c program that Removes multiple spaces between words in a paragraph?
What the advantages of using Unions?
String Processing --- Write out a function that prints out all the permutations of a string. For example, abc would give you abc, acb, bac, bca, cab, cba.?
Can we convert an unsigned long integer value to a string?
What about strpbrk( )?
What about ceil( ) and floor( )?
Why is it not possible to scan strings from keyboard in case of array of pointers to string?