Tyler
Tyler

Reputation: 1

I need to add string characters in C. A + B must = C. Literally

I am writing a program that is due tonight at midnight, and I am utterly stuck. The program is written in C, and takes input from the user in the form SOS where S = a string of characters, O = an operator (I.E. '+', '-', '*', '/'). The example input and output in the book is the following:

Input> abc+aab

Output: abc + aab => bce

And that's literally, not variable. Like, a + a must = b.

What is the code to do this operation? I will post the code I have so far, however all it does is take the input and divide it between each part.

#include <stdio.h>
#include <string.h>

int main() {
  system("clear");
  char in[20], s1[10], s2[10], o[2], ans[15];

  while(1) {
    printf("\nInput> ");
    scanf("%s", in);
    if (in[0] == 'q' && in[1] == 'u' && in[2] == 'i' && in[3] == 't') {
      system("clear");
      return 0;
    }
    int i, hold, breakNum;
    for (i = 0; i < 20; i++) {
      if (in[i] == '+' || in[i] == '-' || in[i] == '/' || in[i] == '*') {
        hold = i;
      }
      if (in[i] == '\0') {
        breakNum = i;
      }
    }
    int j;
    for (j = 0; j < hold; j++) {
      s1[j] = in[j];
    }
    s1[hold] = '\0';
    o[0] = in[hold];
    o[1] = '\0';
    int k;
    int l = 0;
    for (k = (hold + 1); k < breakNum; k++) {
      s2[l] = in[k];
      l++;
    }
    s2[breakNum] = '\0';
    printf("%s %s %s =>\n", s1, o, s2);
  }
}

Upvotes: 0

Views: 150

Answers (3)

Bob.
Bob.

Reputation: 4002

If we assume, from your example answer, that a is going to be the representation of 1, then you can find the representation values of all the other values and subtract the value representation of a from it.

 for (i = 0; i < str_len; i++) {
      int s1Int = (int)s1[i];
      int s2Int = (int)s1[i];
      int addAmount = 1 + abs((int)'a' - s2Int);
      output[i] = (char)(s1Int + addAmount)
 }

Steps

1) For the length of the s1 or s2

2) Retrieve the decimal value of the first char

3) Retrieve the decimal value of the second char

4) Find the difference between the letter a (97) and the second char + 1 <-- assuming a is the representation of 1

5) Add the difference to the s1 char and convert the decimal representation back to a character.

Example 1:

if S1 char is a, S2 char is b:

s1Int = 97

s2Int = 98

addAmount = abs((int)'a' - s2Int)) = 1 + abs(97 - 98) = 2

output = s1Int + addAmount = 97 + 2 = 99 = c

Example 2:

if S1 char is c, S2 char is a:

s1Int = 99

s2Int = 97

addAmount = abs((int)'a' - s2Int)) = 1 + abs(97 - 97) = 1

output = s1Int + addAmount = 99 + 1 = 100 = d

Upvotes: 0

Eric Postpischil
Eric Postpischil

Reputation: 222933

Based on your examples, it appears “a” acts like 1, “b” acts like 2, and so on. Given this, you can perform the arithmetic on individual characters like this:

// Map character from first string to an integer.
int c1 = s1[j] - 'a' + 1;

// Map character from second string to an integer.
int c2 = s2[j] - 'a' + 1;

// Perform operation.
int result = c1 + c2;

// Map result to a character.
char c = result - 1 + 'a';

There are some things you have to add to this:

  • You have to put this in a loop, to do it for each character in the strings.
  • You have to vary the operation according to the operator specified in the input.
  • You have to do something with each result, likely printing it.
  • You have to do something about results that extended beyond the alphabet, like “y+y”, “a-b”, or “a/b”.

Upvotes: 0

Bob Kaufman
Bob Kaufman

Reputation: 12825

Since this is homework, let's focus on how to solve this, rather than providing a bunch of code which I suspect your instructor would frown upon.

First, don't do everything from within the main() function. Break it up into smaller functions each of which do part of the task.

Second, break the task into its component pieces and write out the pseudocode:

while ( 1 )
{
    // read input "abc + def"
    // convert input into tokens "abc", "+", "def"
    // evaluate tokens 1 and 3 as operands ("abc" -> 123, "def" -> 456)
    // perform the operation indicated by token 2
    // format the result as a series of characters (579 -> "egi")
}

Finally, write each of the functions. Of course, if you stumble upon roadblocks along the way, be sure to come back to ask your specific questions.

Upvotes: 3

Related Questions