ty Elysia however it wasnt the answer i need!
i change the code and now it seems to work
the problem was in line 81 to 85 i thought making last item in string ' \0' could resize the string... !
here is mynew code and it still it need some changes like using initializer_list...
Code:
#include <iostream>
#include<windows.h>
#include <stdarg.h>
#include <stdio.h>
#include<string>
using namespace std;
string string_print(string a,...);
string string_print(string a,...)
{
string result;
int i=-1,arg_num=0,T_str=-1;
int size;
/******calculate arg numbers */
while(a[i++]!='\0')
if(a[i]=='%')
arg_num++;
va_list listptr;
va_start(listptr,arg_num);
/******start puting into string*/
for(i=0;i<arg_num;i++)
while(a[T_str++]!='\0')
if(a[T_str]=='%')
{
if(a[T_str+1]=='d') /****** %d */
{
int number=va_arg(listptr,int);
if(number<10)
{
a[T_str]= number+'0';
size=a.size();//because the size change after put string
a.append(" ");
for(i=T_str+1;i<size;i++) //move one character left example: ob %5 f-->ob 5 f
a[i]=a[i+1];
a.resize(size-1);
}
else //for number between 10 and 100
{
int second_digit=number%10;
int first_digit=number/10;
a[T_str]=first_digit+'0';
a[T_str+1]=second_digit+'0';
}
}
if(a[T_str+1]=='c')
{
a[T_str]= va_arg(listptr,int);
size=a.size();
a.append(" ");
for(i=T_str+1;i<size;i++) //move array one character left
a[i]=a[i+1];
}
if(a[T_str+1]=='s')
{
int f_e=0;
int size_a,size_input,c_size_input;
string input;
/***put given string at the end of a and get the input size*/
input=va_arg(listptr,char*);
size_input=strlen(input.c_str());
if(input[0]=='\0')// example "%s%s%d" ,"","dsd",3
{
int start=T_str;
int end=a.size();
while(start<end)
{
a[sh]=a[sh+2];
start++;
}
a.resize(start-2);
T_str=T_str-2;;
}
else
{
a.append(input.c_str());
/************************************/
size_a=strlen(a.c_str());//get new size
c_size_input=size_input;//c:copy
/***************put s equal to input size example %s,dfg-->%sss*******/
while(c_size_input>1)
{
a.append(" "); //add 1 to string size
c_size_input--;
for(i=size_a+f_e;i>T_str+1;i--)
a[i]=a[i-1];
f_e++;
}
/***************put input item instead of s*******/
size_a=strlen(a.c_str());
c_size_input=size_input;
int first_ch=size_a-size_input; //value of first input character
for(int first_put=T_str+1;c_size_input>0;c_size_input--)
a[first_put++]=a[first_ch++];
/***************make extra items in a NULL*******/
int first_null=size_a-size_input;
char last_one_back=a[first_null-1];
a.resize (first_null-1);
a[first_null-1]=last_one_back;
/***************move one character left example %fd d-->fd d**********/
for(i=T_str;i<size_a;i++)
a[i]=a[i+1];
}//end of else
}//end of if
}//end of while
va_end(listptr );
return a;
}
int main()
{
string a="%s%d";
string b=string_print(a,"dsd",32);
cout<<b<<"X";
return 0;
}//12 1