views:

51

answers:

2
while (1)
{
    char j;
    if (x[j] == y[j])

Here I am trying to start a loop where I want to able to match any of the characters from char array 'x' with char array 'y'. If the characters do match from x to y then I want to keep them as they are and if they don't I want to be able to replace them with a star '*'. (e.i. x = [a,p,f] and y = [a,p,p,l,e] and so after the match up and replacement y = [a,p,p,*,*] and when I cout it spells out app**)

I have no idea how to set this up and what type of loop I should use. I fairly new to programming and I know basic replace and switch functions.

A: 

This would be easier if you were using the C++ string class, but here's an answer for C strings.

int i = 0;
while (x[i] != '\0' && y[i] != '\0') {
    if (x[i] != y[i]) {
        y[i] = '*';
    }
    i++;
}
y[i] = '\0'; // C strings are terminated with the null character

Edit: I noticed you wanted to change the y array rather than create a new one.

procrastinate_later
Wouldn't this match the arrays only one index at a time as i increases?
LooneyTunes
Yes. Do you want some different behavior? What if x=['a','b','c','d','\0'] and y=['a','p','c','d','\0']? Then do you want the result ['a','*','c','d','\0'] as I supposed or ['a','*','*','*','\0'], only keeping the beginning characters that match?
procrastinate_later
Under this interpretation of the question (which was my first pass at it), you also need to zap the extra characters. Your code produces 'ap*' rather than 'app**' on the example data. You need to add a loop after this to zap the extra characters.
Jonathan Leffler
@procrastinate, Yea that is what I meant. If x=['a','b','c','d','\0'] and y=['a','p','c','d','\0'] the result would be ['a','*','c','d','\0']. In that case I would have to build a loop that checks all the indexes in y with the first index in x and then all the indexes in y with the second index in x and so on. @Jonathan Leffler, I feel will this need a loop within a loop instead of having it afterwards.
LooneyTunes
A: 

This more or less does what you specify, I think.

#include <string.h>

for (int j = 0; y[j] != '\0'; j++)
{
    if (strchr(x, y[j]) == 0)
        y[j] = '*';
}

Test program

@LooneyTunes asks what happens with: x[] = "apcd" and y[] = "abcd" - do you get "a*cd". The answer is yes. Here's a test program that demonstrates the results. As far as I am concerned, it is pure C code, though G++ is quite happy with it too. You might need the C99 option such as '-std=c99' with GCC set on the compiler. MSVC won't like it if it compiles this as C code; declare j at the top of the function for it.

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

static void doit(const char *x, char *y)
{
    printf("Before: x = %s, y = %s\n", x, y);
    for (int j = 0; y[j] != '\0'; j++)
    {
        if (strchr(x, y[j]) == 0)
            y[j] = '*';
    }
    printf("After:  x = %s, y = %s\n", x, y);
}

int main(void)
{
    const char x1[] = "apf";
    const char x2[] = "apcd";
    char       y1[] = "apple";
    char       y2[] = "abcd";

    doit(x1, y1);
    doit(x2, y2);
    return 0;
}

Output

Before: x = apf, y = apple
After:  x = apf, y = app**
Before: x = apcd, y = abcd
After:  x = apcd, y = a*cd
Jonathan Leffler
If this is C++ code, I think you should "#include <cstring>" and "using std::strchr" rather than "#include <string.h>. Also, strchr() searches for a first occurrence of a character and returns 0 if it's not found. Wouldn't this just see whether each character in y is somewhere in x regardless of its position?
procrastinate_later
@procrastinate: By including <string.h>, I didn't have to write 'std::strchr' or 'using std;'. You're correct, though; it would be better to use the C++ headers. The code looks to see if the current character in y (apple) that I'm looking at appears in the list in x (apf). I was misled by the question at first (the '`x[j] == y[j]`' bit is particularly misleading), but if you look at the expected output, I think you will see that what is implemented is what is requested.
Jonathan Leffler
@Jonathan Leffler, So does this loop do the "If x=['a','b','c','d','\0'] and y=['a','p','c','d','\0'] the result iswould be ['a','*','c','d','\0']" function?
LooneyTunes
@Jonathan Leffler That is just the function I needed. Many Thanks.
LooneyTunes