CoolInterview.com - World's Largest Collection of Interview Questions
Send Free SMS
 Interview Questions  
 Our Services  


INTERVIEW QUESTIONS LANGUAGES C DETAILS
Question :
How do you print only part of a string?
Category C Interview Questions
Rating (2.6) By 10 users
Added on 10/22/2004
Views 4199
Rate it!
Answers:

/* Use printf() to print the first 11 characters of source_str. */

printf(?First 11 characters: ?%11.11s?n?, source_str);



by using extract method we can extract a part from the string
strextract(3,7)



 Posted by: vani    

Contact vani  Contact vani

this is an uncomplied version....

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()
}



 Posted by: rohan sekhri    

Contact rohan sekhri  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);

}



 Posted by: SPN    

Contact SPN  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"



 Posted by: Ravi A Joshi    

Contact Ravi A Joshi  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;
}







}



 Posted by: RAJU SAHANI    

Contact RAJU SAHANI  Contact RAJU SAHANI


If you have the better answer, then send it to us. We will display your answer after the approval.
Name :*
Email Id :*
Answer :*
Verification Code Code Image - Please contact webmaster if you have problems seeing this image code Not readable? Load New Code
Process Verification  Enter the above shown code:*
Inform me about updated answers to this question

   
Related Questions
View Answer
What is indirection?
View Answer
How many levels of pointers can you have?
View Answer
What is a null pointer?
View Answer
What is a void pointer?
View Answer
Is NULL always defined as 0?
View Answer
What does it mean when a pointer is used in an if statement?
View Answer
Can you add pointers together? Why would you?
View Answer
How do you use a pointer to a function?
View Answer
When would you use a pointer to a function?
View Answer
Why should we assign NULL to the elements (pointer) after freeing them?
View Answer

Please Note: We keep on updating better answers to this site. Subscribe to our newsletter to get notified when better answer is posted.

Notify me when better answer is posted!
Email:

View ALL C Interview Questions

User Options
Sponsored Links


Copyright ©2003-2010 CoolInterview.com, All Rights Reserved.
Privacy Policy | Terms and Conditions
Page URL: http://www.coolinterview.com/interview/1012/default.asp?cachecommand=bypass


Download Yahoo Messenger | Placement Papers| FREE SMS | ASP .Net Tutorial | Web Hosting | Free SMS | Dedicated Servers | Joke of the Day

0.61