Question:
How do you print only part of a string?
Answer:
/* Use printf() to print the first 11 characters of source_str. */
printf(“First 11 characters: ‘%11.11s’n”, source_str); Source: CoolInterview.com
by using extract method we can extract a part from the string strextract(3,7) Source: CoolInterview.com
Answered by: vani | Date:
| Contact vani
int i=0,j=0,p; char *s; printf("Enter the String"); scanf("%s",&s); printf(" Enter the start and end of the string: "); scanf("%d %d",&i,&j); for(p=i;p<=j;p++) { printf("%c",*(s+p); } getch() } Source: CoolInterview.com
Answered by: rohan sekhri | Date:
| Contact rohan sekhri
Another solution would be to use strncpy() to copy the part of the string and print it. Below is the code snippet.
int main() { char a1[20]; char a2[20];
strcpy(a1,"HelloWorld"); printf("%s ",a1);
strncpy(a2,a1,5); printf("%s ",a2);
} Source: CoolInterview.com
Answered by: SPN | Date: 5/26/2009
| Contact SPN
The following is much simpler method of printing part of a string.
int main(void) { char *ptr = "This is a test string"; /* partial string printing */ printf("String = %s ", &ptr[10]); return 0; }
This prints: "test string"
Source: CoolInterview.com
Answered by: Ravi A Joshi | Date: 6/28/2009
| Contact Ravi A Joshi
#include"string.h" void main() { int st_index; int end_index; char *str; char* substr(char*,int,int); printf("enter string"); scanf("%s",str); printf("enter start index and end index:"); scanf("%d %d",&st_index,&end_index); printf("the substring is: %s",substr(str,st_index,end_index)); //end of main } //function definition substr(char *temp,int temp1,int temp2) { int i,j=0; char *substr; for(i=temp1;i<=temp2;i++) *substr[j++]=*temp[i]; *substr[j+1]='/0'; return substr; }
} Source: CoolInterview.com
Answered by: RAJU SAHANI | Date: 2/10/2010
| Contact RAJU SAHANI
#include<stdio.h> main() { char str[20]; printf("enter the string"); gets(str); /*the following code prints the string with leaving 5 charaters from starting*/ printf(+5"%c",str); } Source: CoolInterview.com
Answered by: kratika omer | Date: 7/16/2010
| Contact kratika omer
i am sending correct code
main() { int i,j,p; char r[20]; printf("Enter the String"); scanf("%s",&r); char* s; s=r; printf("Enter the start and end of the string: "); scanf("%d %d",&i,&j); for(p=i;p<=j;p++) { printf("%c",*(s+p)); } getch(); } Source: CoolInterview.com
Answered by: shashank kumar | Date: 7/29/2010
| Contact shashank kumar
void spart(char*,int,int); main() { int i,j,p; char str[20]; printf("Enter the String "); gets(str); printf("Enter the start and end of the string: "); scanf("%d %d",&i,&j); spart(str,i,j); getch(); }
void spart(char*p,int x,int y) { int z; for(z=x;z<=y;z++) printf("%c",*(p+z)); } Source: CoolInterview.com
Answered by: shashank kumar | Date: 7/29/2010
| Contact shashank kumar
If you have the better answer, then send it to us. We will display your answer after the approval.
Rules to Post Answers in CoolInterview.com:-
- There should not be any Spelling Mistakes.
- There should not be any Gramatical Errors.
- Answers must not contain any bad words.
- Answers should not be the repeat of same answer, already approved.
- Answer should be complete in itself.
|