Thread: string copy

  1. #1
    Registered User
    Join Date
    Aug 2010

    string copy

    if i have an input which is a string, how do i copy some of the string value from the string? For example 1+5-(2*3)+9, i wan to copy (2*3) from this string, how to do that?

  2. #2
    Registered User
    Join Date
    Sep 2006
    What I do is scan the string, using a while loop, and having two parameters (int's) to mark off the left and right parentheses.

    #include <stdio.h>
    #include <string.h>
    void getSub(char *str, char *sub, int, int); 
    int main() {
      int i, j, n, leftpar, rightpar; 
      char str[40]={"1+5-(2*3)+9"}; //9
      char sub[40]={""};
      getSub(str, sub, leftpar, rightpar);
      printf("\n new string is: %s", str);      //check the string
      printf("\n sub expression is: %s", sub); //and the sub
      printf("\n\n\t\t\t     press enter when ready");
      i = getchar(); ++i;
      return 0;
    void getSub(char *str, char *sub, int leftpar, int rightpar) {
      int i;
      while(str[i] != ')' && str[i] != '\0' ) {
        if(str[i] == '(' )
          leftpar = i;
      str[leftpar++]=' ';   //removes the '(' from the str
      str[rightpar]=' ';      //removes the ')'   "     "     "
      memcpy(sub, (str+leftpar), rightpar-leftpar); //copies our expression into sub
    This doesn't do any actual arithmetic - so far, it just gets the sub expression.
    Last edited by Adak; 09-02-2010 at 04:49 AM.

  3. #3
    Registered User
    Join Date
    May 2010
    adak, Don't give OP the whole code. Let them show their attempt first.

  4. #4
    Registered User
    Join Date
    Aug 2010
    give me the whole code doesnt help me very much bayint, im just wan the idea only.
    Adak, can just show me on the function how strncpy work on this by copy (2*3) onli? Cause im not really clear about the parameters n function work accurately

  5. #5
    Registered User
    Join Date
    Sep 2006
    Quote Originally Posted by Bayint Naung View Post
    adak, Don't give OP the whole code. Let them show their attempt first.
    Hi Bayint.

    The OP already has posted code on this, but it was in his earlier thread:
    parentheses solver

    I have offered more code help than normal, because his school/teacher has asked him to do a job that they have not prepared him for.

    It's not that he doesn't know how to code up a stack, (which would be the preferred way to do this, certainly), it's that he didn't know what the devil a stack was - and yeah, I put a large responsibility for that, on the school/teacher.

    I know what you mean, however. It's always possible I'm being "played" by a slacker, and his cross posting is a concern in that regard.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polymorphism and generic lists
    By Shibby3 in forum C# Programming
    Replies: 9
    Last Post: 07-26-2010, 05:27 AM
  2. Help for my assigment
    By cloverdagreat in forum C Programming
    Replies: 16
    Last Post: 11-21-2009, 12:18 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  5. Replies: 3
    Last Post: 11-03-2002, 02:14 AM