-1
#include "stdafx.h"
#include <stdio.h>
#include "iostream"

char s[100];
char s1[100];

int i = 0;
int i1 = 0;
int i2 = 0;

void populate();
void squeeze();

int main()
{
  printf("Write something, hit enter and than write something again. Hit 
  enter to see result\n");

  populate();
  squeeze();

  system("pause");

  return 0;
}

void populate()     // insert chars in two arrays
{
  int c;

  while ((c = getchar()) != '\n')
  {
      s[i] = c;
      i++;
  }

  while ((c = getchar()) != '\n')
  {
      s1[i1] = c;
      i1++;
  }
}

void squeeze()      // iterate the arrays to find if there are same chars
{
  char s2[1000];
  i = 0;
  i1 = 0;

  for (int r = 0; r <= s[i - 1]; r++)
  {
      for (int j = 0; j <= s1[i1 - 1]; j++)
      {
        if (s[r] == s1[j])
        {
            s2[i2] = s[r];
            i2++;
        }
      }
  }
  for (int m = 0; m <= s2[i2 - 1]; m++)    //print every element in s2[];
  {
      printf("%c", s2[m]);
  }
}

I want to iterate through the second array s1 for every element in the first array s and put the duplicates in the third array s2.
For all I found on the net by far is how to iterate through two or more arrays in the same position arr1[i] = arr2[i], arr1[i1] = arr2[i1] and so on. This is my code, but from unknown reasons for me it does not work.

Cœur
  • 37,241
  • 25
  • 195
  • 267
i3aze
  • 1
  • 5
  • Typo: your `i1 = 0` should be `i2 = 0` in `squeeze()`. By the way `a <= b - 1` is the same as `a < b` for integers – Déjà vu Dec 09 '17 at 09:19
  • Avoid the use of *global variables* unless absolutely necessary. (which when learning C is never) Globals increase the risk for name collisions and variable shadowing problems. This becomes more acute as your code grows in size. Instead, declare needed variables in `main()` and pass them as parameters as required. (it's also a good idea to add the `-Wshadow` option to your compile string) While globals have their place, their use should be a rare exception rather than the rule. – David C. Rankin Dec 09 '17 at 09:43
  • Thanks, I'll keep that in mind – i3aze Dec 09 '17 at 09:46
  • You should write `#include ` instead of `#include "iostream"`. – SurvivalMachine Dec 09 '17 at 17:21

1 Answers1

1
  i = 0;
  i1 = 0;

You are losing the length of the arrays s and s1. And then you access array index out of bound invoking Undefined behavior.(in squeeze).

The correct way to do it would be

void squeeze(void)      // iterate the arrays to find if there are same chars
{
  char s2[1000];

  for (size_t r = 0; r < i; r++)
  {
      for (size_t j = 0; j < i1; j++)
      {
        if (s[r] == s1[j])
        {
            s2[i2] = s[r];
            i2++;
        }
      }
  }
  for (size_t m = 0; m < i2; m++)    //print every element in s2[];
  {
      printf("%c", s2[m]);
  }
}

Few points to note:

  • Use global variables are not needed here. You yourself made the mistake of modifying a global variable. With time in large codes using too many global variables makes it less maintainable and readable.

  • Know how to pass parameter in a function. And also look into functions like fgets which serves the purpose of input in your case.

  • Check these two resources. These will help you a lot

    1. how-to-debug-small-programs
    2. Definitive C books
user2736738
  • 30,591
  • 5
  • 42
  • 56