deepak
BAN USERSE
Ankit, we have changed string of 8 characters ("aabbcdcd" )..then for 9th position , it will not print anyhting. we can just print characters from 0 to 7 range in this string . For big string you also need to change the string in main function.Please do that way.
- deepak September 09, 2012I think this C Code will work. If any issue , please post your views.
#include<stdio.h>
#include<malloc.h>
char explore_string(char *str, int dis)
{
char *p= malloc(sizeof(char));
char *temp = str;
int i=0,count=0,j=0,k=0;
char ch;
char *save = malloc(sizeof(char));
while(*temp != '\0')
{
if(isdigit(*temp))
{
count = *temp - '0';
while(--count)
{
k=0;
while(k<= j-1)
{
*(p+i) = *(save+k);
k++;
i++;
}
}
j=0;
}
else
{
*(save+j) = *temp;
j++;
*(p+i) = *temp;
i++;
}
temp++;
}
*(p+i) = '\0';
printf("Changed string is \n %s\n",p);
ch = *(p+dis);
free(p);
free(save);
return ch;
}
int main(void)
{
char *str = "a2b2cd2";
char ch;
ch = explore_string(str,6);
printf("Required character = %c\n",ch);
return 0;
}
u can just modify main function accordingly.
- deepak September 09, 2012