iSxckk
iSxckk

Reputation: 37

Weird characters appear in C program

This is my program:

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

int main(void) {

    int i;
    char zin[100], combinatie[5];
    int combinaties = 0; // variabele aanmaken om later het aantal conbinaties te tellen

    int letters = strlen(combinatie); // Het aantal letters
    printf("Voer de zin in waarin u de lettercombinatie wilt testen: "); // De zin waarin de combinatie getest zal worden
    scanf("%[^\n]", &zin);
    printf("%c", zin);
    int lengteZin = strlen(zin); // De lengte van de zin

    printf("Voer de combinatie in waarop u wilt zoeken: "); // Combinatie waarop gezocht moet worden
    scanf("\n%[^\n]", &combinatie);

    printf("%s\n", zin); // Hier print je de zin
    printf("%s\n", combinatie); // Hier print je de combinatie

    for (i = 0; i < lengteZin; i++) {
        printf("zin \t %c \n", (zin + i));
        if (*(zin + i ) == (*combinatie)) {
            if (*(zin + i + 1) == *(combinatie + 1)) {
                combinaties++;
            }
       }
    }

    // Hierboven in de voor print je de zin letter voor letter uit, ook check je hier op combinaties door gebruik te maken van de astrix "" die voor pointers staan.

    printf("De lettercombinatie '%s' komt %d keer voor", combinatie, combinaties); // Print het uiteindelijke resultaat uit
    return 0;
}

I am showing certain letter combinations. You can choose which ones you can see. For example in the sentence This is a teest seentence choose the combination ee it will tell you it appears 2 times. So it works!

The question now is: why does it show me weird characters in the console?

I am trying to do it with pointers.

This is what i mean

Upvotes: 0

Views: 936

Answers (3)

chqrlie
chqrlie

Reputation: 144550

The reason you get weird characters is you pass the array zin to printf for the "%c" format instead of the first character zin[0]: in printf("%c", zin); and printf("zin \t %c \n", (zin + i));

The behavior is actually undefined, which is you case produces weird characters, that happen to correspond to the low order byte of the address of zin[i].

Note also that your code can only handle 2 letter strings in combinatie and will report 3 occurrences of ee in heeeello!. You can use the standard function strstr() for a more general approach.

Here is a modified version:

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

int main(void) {
    char zin[100], combinatie[5];
    int combinaties = 0; // variabele aanmaken om later het aantal conbinaties te tellen

    printf("Voer de zin in waarin u de lettercombinatie wilt testen: "); // De zin waarin de combinatie getest zal worden
    if (scanf("%99[^\n]", zin) != 1)
        return 1;

    printf("Voer de combinatie in waarop u wilt zoeken: "); // Combinatie waarop gezocht moet worden
    if (scanf("\n%4[^\n]", combinatie) != 1)
        return 1;

    printf("%s\n", zin); // Hier print je de zin
    printf("%s\n", combinatie); // Hier print je de combinatie

    for (int i = 0; zin[i] != '\0'; i++) {
        printf("zin \t %c \n", zin[i]);
        if (zin[i] == combinatie[0]) {
            if (zin[i + 1] == combinatie[1]) {
                combinaties++;
            }
        }
    }

    // Hierboven in de voor print je de zin letter voor letter uit, ook check je hier op combinaties door gebruik te maken van de astrix "" die voor pointers staan.

    printf("De lettercombinatie '%s' komt %d keer voor\n", combinatie, combinaties); // Print het uiteindelijke resultaat uit
    return 0;
}

Upvotes: 2

iSxckk
iSxckk

Reputation: 37

Thanks for all the answers. It is fixed now.

First

I had a line that didn't do anything. Line 13 "printf("%c", zin);"

This fixed my problem at the start of line 2 of the picture.

second

I forgot to use an astrix (*) at line 25 of the code printf("zin \t %c \n", (zin + i)); in front of (zin + i)); after putting an astrix in front of it. My code was completly fuctional without any problems.

This fixed my problem from the picture where i circled all the characters

Upvotes: 1

virtualpanda
virtualpanda

Reputation: 59

char zin[100]
scanf("%[^\n]", &zin);
printf("zin \t %c \n", (zin + i));

zin array has a length of 100, while the string itself has a length less than 100, so the rest contains random values.

Upvotes: 0

Related Questions