0

I'm a first year student so I'm still struggling with C language.

One of the assignments want's me to make a program where user inputs a word then a letter which the program will use to find all of the letters positions in that word (lets say he enters 'world' and 'r' so the script prints out 'r positions are in 3' / or for 'worldr' 'r positions are in 3 and 6' / or for 'worldrr' 'r positions are in 3 6 and 7').

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


int main()
{

char word[20];

    printf("Enter a word: ");
    scanf("%s", word);

    char letter;
    printf("Enter a letter: ");
    scanf(" %c", letter);

    for(int i = 0; i < strlen(word); i++){

        if(word[i] == letter){

            printf(" %c positions are in %d", letter, i+1);

        }

    }



}

ALSO the program skips "scanf(" %c", letter);" part (error but nothing in logs) if I write %c without the space in the beginning "scanf("%c", letter);" like so. Why?

Basically If I write if(word[i] == 'r'){ instead of if(word[i] == letter){ it works just fine but It won't work like I need it to. I've searched for a solution but no luck other than to try strchr but I need it to print out the array position not the actual memory ram position or something like that or maybe I just don't quite get it yet since I haven't used it before ever. If so I would greatly appreciate If someone could explain me how to solve my problem using if(word[i] == letter){ and strchr ways. I'd love to understand both.

Thank you in advanced!

Alex
  • 39
  • 5
  • 2
    Most `scanf` conversions (e.g. "%d", "%f", "%s") will skip leading whitespace, but "%c" does not. So the space in `" %c"` requests that `scanf` skip any leading whitespace. You need that because otherwise the "%c" will read the newline character that the preceding "%s" left behind. – user3386109 Dec 08 '20 at 01:01
  • 1
    Just as a side note: It is unsafe to use `scanf` without checking the return value. See this page for further information: [A beginners' guide away from scanf()](http://sekrit.de/webdocs/c/beginners-guide-away-from-scanf.html) That link also explains why it may be better to use `fgets` instead of `scanf` in your situation. – Andreas Wenzel Dec 08 '20 at 01:08
  • 1
    The line `scanf(" %c", letter);` must be changed to `scanf(" %c", &letter);`. This is because `scanf` needs to know the address of the variable to write the result to, so that it can write to that address. By omitting the `&`, you are instead passing the current value of `letter` to `scanf`, which is useless information for `scanf`. It is not necessary to use `&` in the other `scanf` statement, because the expression `word` [automatically decays](https://stackoverflow.com/q/1461432/12149471) to `&word[0]`. – Andreas Wenzel Dec 08 '20 at 19:01

1 Answers1

0

Alright to anyone who comes across this issue:

Apparently strlen(word) in the if loop didn't work that was the problem. To fix it I had to create int lenght = strlen(word); ABOVE char letter; and it's important for some reason I don't yet understand. Also changed the letter variable to char letter[1]; with scanf to scanf("%s", letter);.

Alex
  • 39
  • 5
  • 1
    The proper solution would have been to write `&letter` instead of simply `letter` in the `scanf` function call. See my other comment for further information. By changing the declaration of `letter` to an array, you are accomplishing nothing more than making the expression `letter` [automatically decay](https://stackoverflow.com/q/1461432/12149471) to an address. This is not necessary. – Andreas Wenzel Dec 08 '20 at 18:57