-1
#include <stdio.h>
#include <stdlib.h>
#define can 100
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
 void hesaplacumle(int *sayi,char dizi[can])
 {
    *sayi=strlen(dizi);
    printf("Karakter dizimin boyu: %d\n",*sayi);
    
 }
 void ortakhesap(int *ortak,char dizi1[can])
 {
     *ortak=0;
 int i=0;
  int konum=0;
  int num1[26]={0};
  int num2[26]={0};
  while(dizi1[i])
  {
    if(dizi1[i]==32)
      {
        break;
      }
    else if(dizi1[i]>96 && dizi1[i]<123)
    {
        konum=dizi1[i]-'a';
        num1[konum]++;
    }
    i++;
  }
   i+=1;
    while(dizi1[i])
  {
    
    if(dizi1[i]>96 && dizi1[i]<123)
    {
        konum=dizi1[i]-'a';
        num2[konum]++;
    }
    else{
        break;
    }
     
     
    i++;
  }

 
    for(i=0;i<26;i++)
    {
    if(num1[i]!=0 && num2[i]!=0)
    {
        *ortak++;
    }
    }
    printf("2 kelimenin ortak harf sayisi: %d",*ortak);
 }
int main(int argc, char *argv[]) 
{
    int ortak;
  char dizi[100];
  char dizi1[100];
  int sayi;
  printf("\nLutfen bir cumle giriniz: \n");
  gets(dizi);
  hesaplacumle(&sayi,dizi);
  
  printf("\nLutfen iki kelime giriniz: \n\n");
  gets(dizi1);
  
  
  ortakhesap(&ortak,dizi1);
 
  
    
}

hesaplacumle(&sayi,dizi); I was expecting exactly same ouput from this function it is OK.However, ortakhesap(&ortak,dizi1); when I try this one without using function it works properly.On the other hand, when I try it using a function it prodcues same ridiculous outputs which I do not understand.Here my question is what I am missing?Main idea is using addressing method.Thanks for your helps.Due my respects.

hesaplacumle(&sayi,dizi) this one calculates long of the sentences. ortakhesap(&ortak,dizi1); this one calculates common character number between two word. True and wrong output

  • 6
    OT: Never ***ever*** use the `gets` function! It's so [dangerous](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used) that is has even been removed from the C language. Any resource, book, tutorial or class that teaches it should be thrown away. – Some programmer dude Jul 30 '23 at 14:58
  • As for your problem, can you please narrow it down, do we need all that code for it? Is it really a [mre]? And please try to give your functions and variables English names, and try to write English output. It will make it much easier for us to know what's going on. – Some programmer dude Jul 30 '23 at 14:59
  • 2
    Oh and please don't use [magic numbers](https://en.wikipedia.org/wiki/Magic_number_(programming)). If e.g. `dizi1[i]>96 && dizi1[i]<123` is supposed to check for ASCII-encoded lower-case letters, then use the standard [`islower`](https://en.cppreference.com/w/c/string/byte/islower) function instead. Besides making your code cleaner and easier to read, it will also handle non-ASCII encodings. And instead of `dizi1[i]==32` to check for space, check for an actual space: `dizi1[i]==' '`. – Some programmer dude Jul 30 '23 at 15:01
  • 1
    Your code would be *much* easier to read if it consistently used any of the common indentation schemes. – John Bollinger Jul 30 '23 at 15:21

1 Answers1

2

The problem with function ortakhesap() is here:

        *ortak++;

You intend to increment the int to which ortak points, but postfix ++ has higher precedence than the unary * operator, so you are instead incrementing the pointer itself, then dereferencing it and discarding the value. You could achieve what you intended by inserting parentheses ...

        (*ortak)++;

... or by using plussignment instead of the increment operator ...

        *ortak += 1;

. Better, however, would be either to use the function's return value instead of an out parameter, or at least to use a local variable to accumulate the count, and set *ortak only once, at the end.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157