Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Find And Replace?! In String, C Problem

pascal string

  • Please log in to reply
12 replies to this topic

#13 Agent001

Agent001

    CC Regular

  • Member
  • PipPipPip
  • 41 posts

Posted 02 May 2012 - 09:30 AM

Can you try out this bit of code and tell me what you get?
It doesn't seem to work.

Enter any string, like 'test' or whatever.

When i enter test i get 'test=2222' as a result of printf

void main() {
char* str;
char c;
int d=1,i=0;
str=(char*)calloc(1,sizeof(char));
while ( (c=getchar()) != '\n')
{
  d++;
  str = (char*)realloc (str, d);
  str[i]=c;
  i++;
}
printf("%s", str);
free(str);
}

  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download