3 Replies - 227 Views - Last Post: 02 October 2012 - 08:44 PM Rate Topic: -----

#1 NessValdez  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 70
  • Joined: 12-March 12

separating a string into 3 different arrays

Posted 02 October 2012 - 12:10 PM

Here is a section of my program. I have to receive user input in the form of a+b or string, operator, string. I just want to make sure I am splitting the string appropriately.

#include <stdio.h>
#define characterToValue(ch)	((ch)-'a'+1)
#define valueToCharacter(v)	((v)+'A'-1)

int main()
{
	char input[20] = {'\0'};
	char stringA[9] = {0};
	char stringB[9] = {0};
	char result[9] = {0};
	int tempA[9] = {0};
	int tempB[9] = {0};
	char operator;
	int i = 0;

   printf("\n////////////////////////////////////////////////////////////////////////////////////////\n");
   printf("\n   Welcome! This is a pseudo arithmetic program.");
   printf("\n   Use only +-/* operators. Dont use spaces. Each string can contain up to 9 lowercase letters.");
   printf("\n   Example: abc+aab\n");
   printf("\n////////////////////////////////////////////////////////////////////////////////////////\n");
   	
		printf("***********************************************************************************\n");
		printf("Please enter a string expression: ");
  		scanf("%s",input[20]);
	
	// Seperates input into SOS 
	for (i=0; i<20; i++) 
	{
		if (input[i] != ('+' || '-' || '*' || '/')) 
		{
			stringA[i] = input[i];

		}
		else if (input[i] = ('+' || '-' || '*' || '/')) 
		{
			operator = input[i];
		}
			stringB[i] = input[i];
			i++;
	}


Is This A Good Question/Topic? 0
  • +

Replies To: separating a string into 3 different arrays

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3530
  • View blog
  • Posts: 10,933
  • Joined: 05-May 12

Re: separating a string into 3 different arrays

Posted 02 October 2012 - 12:22 PM

Your line 29 is checking to see if input[i] is not equal to one. I don't think that is what you intended.

Your line 34 is assigning 1 to input[i]. Additionally, the if condition will always be true. Again, I don't think this is what you intended.

You should review how Boolean expressions and Boolean operators work.

May I recommend a different approach. Consider this pseudo code:
pick up the first character
while (character is not an operator or end of string)
{
    store the character in string 1
    pick up the next character
}

if (not end of string)
    store the character as the operator

while (character is not an operator or end of string)
{
    store the character in string 2
    pick up the next character
}



Notice the similarity between the two while loops. This can eventually be moved to a function if you have covered functions in your class.
Was This Post Helpful? 1
  • +
  • -

#3 jimblumberg  Icon User is online

  • member icon


Reputation: 4002
  • View blog
  • Posts: 12,346
  • Joined: 25-December 09

Re: separating a string into 3 different arrays

Posted 02 October 2012 - 12:22 PM

The first thing I noticed was this line:
   if (input[i] != ('+' || '-' || '*' || '/')) 

You can't compare a character to multiple characters like this in C. You need both sides of the equation for all the tests. For example:
if(input[i] != '+' || input[i] != '-')

Also I believe your logic is incorrect at any rate. If input[i] is equal to '+' then it will be not equal to anything else, causing your statement to evaluate to true in all cases.

The next thing I noticed is that you can't assign a value to a single character with the following line:
        else if (input[i] = ('+' || '-' || '*' || '/')) 

This is similar to the previous line but this statement will always evaluate to true because you are using the assignment operator= instead of the comparison operator==. Also you don't really need this else if() at all, since you are really just retesting for the previous if statement.

Didn't delve any deeper than that into your code so you may have other issues. But I would recommend getting rid of the two macros you defined. If you need to do that math, then do it in place until you are much more familiar with the language.


Jim
Was This Post Helpful? 1
  • +
  • -

#4 NessValdez  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 70
  • Joined: 12-March 12

Re: separating a string into 3 different arrays

Posted 02 October 2012 - 08:44 PM

Thanks for the input!! I got my assignment done and working.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1