Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 946239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:53:00+00:00 2026-05-15T22:53:00+00:00

while (1) { char j; if (x[j] == y[j]) Here I am trying to

  • 0
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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T22:53:01+00:00Added an answer on May 15, 2026 at 10:53 pm

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.