   Untitled
  I need to extract every  10 characters from a string. Following is the code.But output is quite unambigious.int ilength = 10;char* string = "This is test program for extracting every 10th word";for(char* temp=string; temp!=NULL; temp+=ilength){printf("%s\n", (temp + ilength));}Desired output.This is test program for extrac.............etcWhere am I going to wrong.   
  (Assuming C)The problem with the loop above is that temp will pass the end of string (since temp != NULL compares its address with 0x00000000; *temp != NULL only works when the length of the string is a multiple of ten).  temp will then point to memory outside of bounds with undefined results.  You need to assure that temp will not pass the end of the string.Here's how you can do this in C:/* #include <string.h> */const int ilength = 10;const char* string = "I need to extract every 10 characters from a string. Following is the code.";const char* end = string + strlen( string ); /* use strlen to get the length of a string */char buf[ilength + 1];buf[ilength] = '\0'; /* strncpy does not copy null terminator */for(const char* temp=string; temp<end; temp+=ilength){strncpy( buf, temp, ilength ); /* copy ilength chars from temp to buf */printf("%s\n", buf );}(Assuming C++)Try this:// #include <string>// #include <iostream>std::string string = "I need to extract every 10 characters from a string. Following is the code.";const int length = 10;for ( unsigned i = 0, l = string.length(); i < l; i += length )std::cout << string.substr( i, length ) << std::endl; [Edited by - _fastcall on June 25, 2009 2:57:53 AM]