-1
#include <stdio.h>
#include <stdlib.h>

int main()
{
    char A[100];
    char c;
    scanf("%s", &A);
    while ((getchar()) != '\n');
    scanf("%c", &c);
    int i, count = 0;
    for(A[i] = 0; A[i] != c; i++) {
        count++;
    }
    //printf("%d",count);
    for(i = 0; i < count; i++) {
        printf("%c", A[i]);
    }
    return 0;
}

I want to print (ca) if I enter input String A as (cat) and character c as (t ).But I am getting output as (a) the first word is not printing .Please tell me what is wrong with my code.

Weather Vane
  • 33,872
  • 7
  • 36
  • 56
  • 2
    You have not initialized `i;`. The `int i, count = 0;` only initializes `count`, so please avoid multiple declarations like this. – Weather Vane Feb 05 '23 at 09:04
  • But iam still getting the same output – Bharath R Feb 05 '23 at 09:07
  • 3
    I don't understand what `A[i] = 0` is supposed to do. It overwrites a character of the string. Did you mean `for(i = 0; ...`? Note that the loop won't end if the string does not contain `c`. – Weather Vane Feb 05 '23 at 09:11
  • yeah i know but the question given to me is that the string A must contain it contains a letter that c variable holds. – Bharath R Feb 05 '23 at 09:15
  • `scanf("%s", &A);` is wrong. Your compiler should have warned about this. Change it to `scanf("%s", A);` Remember, `A` has type `char [100]`, so when passed as an argument the type is `char *`. You code is passing it as `char (*)[100]`, which is wrong. – Tom Karzes Feb 05 '23 at 09:17
  • 1
    Never, ever, assume that input will be what you are expecting it to be. It should be `A[i] != '\0' && A[i] != c;` You also need to restrict the input with `scanf("%99s", A);` – Weather Vane Feb 05 '23 at 09:19
  • There is another hole too: if the user taps Enter *twice* the code will fail. So use `scanf(" %c", &c);` (with the extra space) to filter *all* whitespace and delete the loop that remove a *single* newline. – Weather Vane Feb 05 '23 at 09:21
  • Does this answer your question [C: Get substring before a certain char](https://stackoverflow.com/questions/15006269/c-get-substring-before-a-certain-char)? – Cristik Feb 06 '23 at 15:48

3 Answers3

1

When you program in C it's very valuable to look at the warnings you get from the compiler. If you use Windows the Visual C compiler should output something like this, assuming your program is stored in test.c:

C:\Users\Public>cl test.c
Microsoft (R) C/C++ Optimizing Compiler Version 19.31.31107 for x86
Copyright (C) Microsoft Corporation.  All rights reserved.

test.c
C:\Users\Public\test.c(12) : warning C4700: uninitialized local variable 'i' used
Microsoft (R) Incremental Linker Version 14.31.31107.0
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:test.exe
test.obj

On Linux, using GCC, you get a similar warning but in this case you need to add the option -Wall which enables many useful warnings:

$ gcc -Wall test.c
test.c: In function ‘main’:
test.c:8:13: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[100]’ [-Wformat=]
    8 |     scanf("%s", &A);
      |            ~^   ~~
      |             |   |
      |             |   char (*)[100]
      |             char *
test.c:12:14: warning: ‘i’ is used uninitialized in this function [-Wuninitialized]
   12 |     for(A[i] = 0; A[i] != c; i++) {
      |         ~~~~~^~~
August Karlstrom
  • 10,773
  • 7
  • 38
  • 60
  • As Stackoverflow mentioned, Bhara R is a new contributor. Be nice, not passively-aggressive. – TopchetoEU Feb 06 '23 at 15:25
  • 1
    @TopchetoEU You are right. It was intended as "teaching someone how to fish" but could be interpreted in the wrong way. I have rephrased my reply now. – August Karlstrom Feb 06 '23 at 15:50
0
for(A[i] = 0; A[i] != c; i++) 

This assigns 0 to the first position in A (A[i] = 0). You probably wanted to write

for(i = 0; A[i] != c; i++) 

instead.

There are some other problems with the code as well, especially in error handling, but I'll let you figure those out.

fishinear
  • 6,101
  • 3
  • 36
  • 84
  • i don't see why you'd leave OP to figure out the other issues. OP is obviously a beginner, so any help would be useful for him. don't gatekeep information – TopchetoEU Feb 06 '23 at 15:56
0

It seems you've made one big mistake, and it's in the first for loop:

for(A[i] = 0; A[i] != c; i++) {
    count++;
}

Here, you're initializing A[i] to 0. Now, let's see what's wrong with that: at that point in the code, i is not initialized, so it has an arbitrary value (like -53451561). This is bad, because you're trying to access the -53451561th element of the array, which is never good. Of course, i could be 0, which means that you'll get away with setting the first character of A to 0, but the point is that you don't know the value of i and shouldn't use it before you initialize it.

Next, this is not really how you write a for loop, and this could very much be a mistake. What you should've written is i = 0. This way you start from i = 0, and use it as a counter in the loop (since the last statement in the for loop is i++). As a rule of thumb, the variable in the first statement of the for is the same as the last one.

Lastly, the condition is correct, but you should account for the end of the string \0. So, the final form of the for loop looks something like this:

for(i = 0; A[i] != c && A[i] != '\0'; i++) {
    count++;
}

Now, there's a minor issue that has been pointed out by other comments: you scanf strings without the reference & operator:

scanf("%s", mystring);

This is because the string is already a pointer itself, so scanf can already write the result to the pointer, without needing a pointer to the pointer.

One more thing:

You could just print the characters in the first loop, which will improve the performance of your program twice.

Finally, this is how you program could look. Please, copy this only if you understand what I've explained above:

char A[100];
char c;
int i;

scanf("%s", A);
while ((getchar()) != '\n');
scanf("%c", &c);

for(A[i] = 0; A[i] != c; i++) {
    printf("%c", A[i]);
}
TopchetoEU
  • 697
  • 5
  • 9