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

  • SEARCH
  • Home
  • 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 8863579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T16:00:53+00:00 2026-06-14T16:00:53+00:00

#include <stdio.h> #define MAX 1000 void any(char s1[], char s2[], char s3[]); int main()

  • 0
#include <stdio.h>
#define MAX 1000
void any(char s1[], char s2[], char s3[]);
int main()
{
    char string1[MAX], string2[MAX], string3[MAX];
    printf("Jepni stringen 1\n");
    scanf("%s", &string1);  //saving string 1
    printf("Jepni stringen 2\n");
    scanf("%s", &string2);  //saving string 2
    any(string1, string2, string3); /*comparing characters from string 2 to string 1    and saving the places where they are equal on third string */
    printf("%d", string3[0]);   //printing the first character of the third string
    return 0;
}

void any(char s1[], char s2[], char s3[])
{
    int i, j, k;
    k = 0;
    for (j = 0; j != '\0'; j++) {
        for (i = 0; i != '\0'; i++) {
            if (s1[i] == s2[j]) {
            s3[k] = i;
            j++;
            k++;
            }
        }
    }
}

I am trying to create a c program that scans 2 strings (saves them on string 1 and 2) than the program using function any will see character by character if the string 2 characters are equal with the string 1,If they are it will give the first position where they are found.In case nothing is found it displays a -1.The program asks for the first character that is equal,thats why i am printing always the first character from string 3.The program isnt working cuz it always prints -1.
Example if i put on string 1 dad
and on string 2 the character d
dhe program should display the 0 position
if i put dad on string 1
and on string 2 i put a
it should display 1.

  • 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-06-14T16:00:54+00:00Added an answer on June 14, 2026 at 4:00 pm

    First of all the for loop doesn’t begin because the condition is that j shall be different from zero.In ASCII ‘\0’ is zero (maybe not on all machines), so you rather want to check that s2[j] is different from zero.Same for i.
    Another thing is that s3 is an array of chars, so putting s3[k]=i doesn’t make it equal to ‘1’ or ‘2’, but to 1 or 2 (ASCII values), so you should add 48 to i (good till ‘9’, then you’ll have two digits), or print the string character per character, with the %d format specifier:

    void any(char s1[], char s2[], char s3[])
    {
        int i, j, k;
        k = 0;
        for (j = 0; s2[j] != '\0'; j++) {
            for (i = 0; s1[i] != '\0'; i++) {
                if (s1[i] == s2[j]) {
                    s3[k] = i;
                    j++;
                    k++;
                }
            }
        }
    }
    

    Maybe I’m missing some other error, try the code and run it to see if it’s right (also remember to use the %d specifier to print s3).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#include <stdio.h> #define MAX 5 int stk[MAX]; int top=-1; main() { char ch; void
#include<conio.h> #include<stdio.h> #define abc 7 int main() { int abc=1; printf(%d,abc); getch(); return 0;
#include stdafx.h #include<stdio.h> #define PR(x) printf(%d\t,(int)(x)); #define PRINT(a,b,c) PR(a) PR(b) PR(c) #define MAX(a,b) (a<b?b:a)
Suppose I define a union like this: #include <stdio.h> int main() { union u
#include<stdio.h> #define N (sizeof(array) / sizeof(array[0])) main() { int array[5]={1,2,3,4,5}; int d; for(d=-1;d <=
#include<stdio.h> #include<iostream.h> #include<conio.h> #include<stdlib.h>(TOP) #include<fstream.h> #define MAX 5 int top = -1; int stack_arr[MAX];
here is my code. #include <stdio.h> #define MAX(a,b) ((a)>(b)?(a):(b)) #define SZA(a) (sizeof(a)/sizeof(a[0])) int anish=0;
#include<stdio.h> #include<stdlib.h> #define MAX 1000 struct island{ double left; //gobal double right; } island[MAX];
#include <stdio.h> #include <stdlib.h> #define MAX 20 #define MAX_BASE 8 #define ROW 9 #define
#include <stdio.h> #include <stdlib.h> #define MAX 21 #define MAX_ELEM 8 #define SCORE 12 #define

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.