0

I am trying to write an application in C and I have a problem: I have some unwanted chars in my string2 array in the code part below:

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

#define max_len 100

int main() {
    char string1[max_len], string2[max_len], string3[max_len], temp[max_len];
    int m, n, i, j, k, min_str_len;
    puts("Enter your first word: ");
    gets(string1);
    puts("Enter your second word: ");
    gets(string2);
    m = strlen(string1);
    n = strlen(string2);
    min_str_len = ((m < n) ? m : n);

for (i = 0; i <= m; i++) {
    for (j = 0; j <= n; j++) {
        for (k = 0; k <= min_str_len; k++) {
            if (string1[i] == string2[j]) {
               //printf("%s",string2[j]);
               temp[k] = string2[j];
               break;
            }
             ...
         }
    }
}

So you see here a part of my code. I am assigning "placozoa" as string1, and "placement" as string2. Please see the line: //printf("%s",string2[j]); When I make this line active, I see:

enter image description here

I did not understand why [] and ' characters are included in string2, I was just expecting to see "plac" there in the output.. The weird thing is, I am printing string2 array right after getting from the user (under gets(string2); line), and I see it is "placement" there.

What is happening in the middle, where is my mistake?

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
Eray Balkanli
  • 7,752
  • 11
  • 48
  • 82

1 Answers1

0

In your print statement, printf("%s",string2[j]); use %c instead of %s,
Because %s always try to convert the printing item to string. And to do that, your compiler has added a null char at the end of it.
So what actually happening right now, when j == 0, string2[j] is equal to 'p' as a single charcter, but as string it becomes "p\0" where \0 is the null character which means the end of a string.
So, I think, just printing as a single character using %c (character) will solve the problem.

Shamil Sakib
  • 140
  • 8
  • 1
    Using `%s` but passing a single `char` does *not* add a null and print it as a string... it may try to interpret the `char` (passed as an `int`) and possibly what follows it as a pointer and then print the characters found at the resulting address (up to the first null character it finds)... and it may not even succeed at that. – Dmitri Nov 17 '16 at 05:04
  • @Dmitri, yes, this code should have shown a segmentation fault in general case. But I think his compiler handled the situation the way I mentioned. Either this code should not even give any output having this `printf("%s",string2[j])` uncommented – Shamil Sakib Nov 17 '16 at 05:12
  • 1
    It makes no sense for the compiler (or printf library function) to do what you've suggested. With `%s`, `printf()` expects a pointer -- but a character was passed -- it would try to interpret the character as a pointer, and print what it points to. It certainly wouldn't convert the types, or add a null character -- and even if it did add a null and somehow print the character passed and not what it seems to point to, it wouldn't display the null character anyway. – Dmitri Nov 17 '16 at 05:18
  • Yeah, that should be a segmentation fault. I am surprised how did he get that output with the print statement. – Shamil Sakib Nov 17 '16 at 05:21