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

#1 Agent001

Agent001

    CC Regular

  • Member
  • PipPipPip
  • 41 posts

Posted 01 May 2012 - 02:56 PM

Hi guys, i need help once again.

I need to make a little program that will do the following:
when a user enters a foor loop in pascal it need's to be converted to a valid for loop in c;
for example
for i:=100 downto a-b
do becomes
for(i=100; i>=a-b; i--);

Can anyone help with the idea, i'm kinda stuck here :)
  • 0

#2 Orjan

Orjan

    CC Mentor

  • Moderator
  • 2918 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#

Posted 01 May 2012 - 04:12 PM

Looks right to me...
  • 0

I'm a System developer at XLENT Consultant Group mainly working with SugarCRM.
Please DO NOT send mail or PM to me with programming questions, post them in the appropriate forum instead, where I and others can answer you.


#3 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 01 May 2012 - 06:00 PM

.... me too :)
  • 0

#4 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts
  • Location:Argentina
  • Programming Language:C, C++, C#, PHP, JavaScript, Transact-SQL, Bash, Others
  • Learning:Java, Others

Posted 01 May 2012 - 07:24 PM

yep, i think there is some info missing, so ... whats the big secret buddy ?
  • 0

#5 Agent001

Agent001

    CC Regular

  • Member
  • PipPipPip
  • 41 posts

Posted 02 May 2012 - 02:28 AM

Maybe i wasn't clear enough :)

I need to write a program, in C, that changes one string to another.
The first string is a for loop in Pascal, and the program must change that string to make it a valid for loop in C, and write it out on the screen.

I hope i made it clearer now.
  • 0

#6 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 02 May 2012 - 03:00 AM

And, what language you want to use to do that? C, C++, C#, Java ... ?
  • 0

#7 sepp2k

sepp2k

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 183 posts
  • Programming Language:C, Java, C++, Python, Ruby, Scheme, Bash, Haskell, Others
  • Learning:Others

Posted 02 May 2012 - 03:03 AM

If the expressions for the start and end value are relatively simple like in your example (i.e. they don't need any translating), the translation will be pretty straight forward. Just extract the variable name, the expression for the start value and the expression for the end value and check whether the direction is "to" or "downto". Then build the corresponding for-loop in C and insert the variable name and the two expression appropriately.
  • 0

#8 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 02 May 2012 - 03:53 AM

Well, I made it in C somewhat following no logic :glare:


#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>

#define BUFFER_LEN 100


void main()
{
char pascal_for_loop[BUFFER_LEN];
int pascal_loop_len;

char varname[BUFFER_LEN];

char c_for_loop[BUFFER_LEN];


char * tmp;
int i, j;

strcpy(pascal_for_loop, "for var := 100 downto a - b");
pascal_loop_len = strlen(pascal_for_loop);

i = 0;
j = 0;
// getting the variable name
while(i < pascal_loop_len) {
if (pascal_for_loop[i] == ':') {
i--;
while (pascal_for_loop[i] == ' ')i--;
while (pascal_for_loop[i] != ' ')varname[j++]=pascal_for_loop[i--];
varname[j] = '\0';
strrev(varname);
break;
}
i++;
}


tmp = strstr (pascal_for_loop,"for");
strncpy (tmp,"for(",4);
tmp = strstr (pascal_for_loop,":=");
strncpy (tmp," ",1);

tmp = strstr (pascal_for_loop,"downto");
strncpy (tmp,"; %s< ",6);

sprintf(c_for_loop, pascal_for_loop, varname);
strcat(c_for_loop, "; ");
strcat(c_for_loop, varname);
strcat(c_for_loop, "--);");

printf(c_for_loop);
}

  • 0

#9 Agent001

Agent001

    CC Regular

  • Member
  • PipPipPip
  • 41 posts

Posted 02 May 2012 - 08:42 AM

Tnx, kernelcoder, i'll make good use of it.

Also, i need to dynamicaly alocate memory for the string, while user enters it's content from the main input.
That is, user needs to type that string
for:=100 downto a-b

and i need to alocate exactly 19+1 bytes.
How do i that? can i do it like this

cntr = 1;
str=malloc(sizeof(char)) // asign memory for one char
while((c=getchar())!='\n') {
string[i]=c;
cntr++;string = realloc(string, cntr) // allocates two bytes, for the character that will be written in the next iteration
}

  • 0

#10 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 02 May 2012 - 09:02 AM

That's right. More accurately, allocating memory of char type is as follows...
char* str= (char*)malloc(sizeof(char));

BTW, don't forget to free the memory at the end of your use. You know, most people don't do that and in turn they suffer from memory leaks :drool:
  • 0

#11 Agent001

Agent001

    CC Regular

  • Member
  • PipPipPip
  • 41 posts

Posted 02 May 2012 - 09:19 AM

Hm, seems that this piece of code doesn't work.
The string always has more elements than enetered o.O?
  • 0

#12 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 02 May 2012 - 09:24 AM

I think it is better to use calloc rather than malloc in your case.
char* str= (char*) calloc (10,sizeof(char)); // will allocate 10 chars

  • 0





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