Splitting a string into different variables

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • reshmidoudou
    New Member
    • Oct 2006
    • 9

    Splitting a string into different variables

    a string contains an equation and I have to break the string into integers and other variables :

    example of the string is "2 + 3 = 5"

    I have tried to use substr but it does not work as I can have "120 - 30 = 90" so the integers are of no fixed length.

    How can I do that, please help!
  • zahidkhan
    New Member
    • Sep 2006
    • 22

    #2
    Originally posted by reshmidoudou
    a string contains an equation and I have to break the string into integers and other variables :

    example of the string is "2 + 3 = 5"

    I have tried to use substr but it does not work as I can have "120 - 30 = 90" so the integers are of no fixed length.

    How can I do that, please help!

    Hi you can use sscanf
    like this
    const char* p = "100-200=300";
    int i,j,k;
    sscanf(p,"%d-%d=%d",&i,&j,&k );

    But make it sure that there should not be any space in your string expression

    Comment

    • anushhprabu
      New Member
      • Sep 2006
      • 43

      #3
      if(isdigit(*exp r_Ite) || isDot(*expr_Ite )) // for numbers and DOT
      {
      while(isdigit(* expr_Ite) || isDot(*expr_Ite ))
      {
      if(isDot(*expr_ Ite)) // Flag to check the occurrence of DOT
      {
      Flag=1;
      }
      else
      {
      Temp1*=10;
      Temp1+=((*expr_ Ite)-48);//-48 is used to get the numbers.. ASCII value for 0 is 48... 9 is 57
      }
      if(Flag==1) ++Temp2; // to count number of digits after DOT
      ++expr_Ite;
      loop++;
      }//while(isDot() || is dot())
      while(Temp2>1) { Temp1=Temp1/10; Temp2--; } // to manipulate the integer into float value
      Value.push(Temp 1);
      }

      Comment

      • vermarajeev
        New Member
        • Aug 2006
        • 180

        #4
        Code:
        int main(int argc, char** argv[])
        {
        	string str;
        	string variable;
        	string integers;
        	cout<<"Enter the string"<<endl;
        	cin>>str;
        
        	int j=0;
        	bool flag = false;
        	for(int i=0; i<strlen(str.data()); ++i)
        	{
        		if(!isdigit(str[i]))
        		{
        			variable.push_back(str[i]);				
        			flag = true;
        		}
        		else
        		{			
        			integers.push_back(str[i]);	
        		}
        		if(flag)
        		{
                   integers.push_back('\n');	
        		   variable.push_back('\n');	
        		   flag = false;
        		}
        	}	
        	cout<<"variable:"<<variable<<endl;
        	cout<<"integers:"<<integers<<endl;
        
        	return 0;
        }

        Comment

        • reshmidoudou
          New Member
          • Oct 2006
          • 9

          #5
          Thanks for the code, guys. Another question, I have to test the maximum input done on the screen, let me explain when keying the input after the 5th input the pgm should display a message like invalid input.

          example : I type the equation

          2 + 3 = 5 (input = 5 )

          but if I type

          2 + 3 = 5 + 0 (input more than 5)

          how do I do the test since the coding is like that@

          int var1, var2 , result;
          string op, eq;

          cin >> var1 >> op >> var2 >> eq >> result;

          I am stuck who can help pls?

          Comment

          Working...