0

the idea is that i type in a sentence and store it as a string... then i can choose a letter and change it to a different letter

int main(int argc, char *argv[])
{

    char string[100];
    char newLetter;
    char oldLetter;
    int i = 0;

    printf("Please enter your sentence : ");
    gets(string);

    printf("\n\nWord is : %s" , string );

    printf("\n\nTarget : ");
    scanf("%s", &oldLetter);
    printf("Replace with : ");
    scanf("%s", &newLetter);

    for ( i = 0; i < sizeof(string); i++) 
    { 
        if (string[i] == oldLetter)
        { 
            string[i] = newLetter; 
            break;
        } 
    }
    printf("\n\nWord is : %s" , string );

    system("PAUSE");   
    return 0;
}

any help in where I've gone wrong is appreciated

eg. input could be - yellow lorry red lorry

then target - r

change to - t

output - yellow lotty ted lotty

Skwal
  • 2,160
  • 2
  • 20
  • 30

3 Answers3

1

Change the %s in the scanning of the two letters to %c and the code will run flawlessly.

reza.safiyat
  • 499
  • 10
  • 21
0

First things first, since you are aiming to store a character to char oldLetter and char newLetter via scanf, you should be using the format specifier %c instead of the %s.

However, that won't be enough, because of the following: When you use functions like scanf or gets, you prompt user to input characters to the stdin stream. stdin stream is a buffered stream. You may think of it as a:

  • Stream of river that you;
  • drop characters into
  • each character you drop remains inside the river
  • until something takes them out

When scanf comes, and you type, say, A and then press the enter key, you put the following characters in the stream:

'A' '\n'

Where \n is the new-line character. With the enter key-press, you also inform the scanf that you're done. scanf then starts issuing the stdin buffer, let's see... 'A', a proper value for %c character. It takes that one out, leaves \n behind.

Then the next scanf comes, seeks for a %c in stream, finds the \n ready, takes that out. This is what you wouldn't want. Two ways to prevent it:

  • use fflush( stdin ); after the scanf calls, or
  • use while( getchar( ) != '\n' ); after the scanf calls

to dismiss/discard the remaining characters in the buffer.

And get rid of that break; if you want to replace each occurrence and not just the first one.

Utkan Gezer
  • 3,009
  • 2
  • 16
  • 29
  • 1
    No. `fflush( stdin )` is undefined behavior. – this Mar 05 '14 at 21:47
  • @self. Dude, OP uses `scanf( "%c", ... )` to get a character from standard input, and you make `fflush( stdin )` a problem? One cannot please everyone, but hey, I tried, and offered two alternatives there. Second one also isn't perfect, I'd say `while ( stdin->_cnt ) getchar( );` would pretty much be perfect, but that would just be an overkill, for the reason at the beginning of this comment. – Utkan Gezer Mar 05 '14 at 22:11
  • Also `while( getchar( ) != '\n' );` should check for != EOF. – this Mar 05 '14 at 22:12
  • Because you don't check for EOF and might miss it, causing an inf loop. – this Mar 05 '14 at 22:24
  • @self. That doesn't explain anything. Explain me why `EOF` should not be missed when each line is terminated by a `'\n'`. – Utkan Gezer Mar 05 '14 at 22:32
  • I'm going to let you figure that out by yourself. – this Mar 05 '14 at 22:34
  • @self. Such a decent help, man, you're incredible. I have to say, *some people are weird.* – Utkan Gezer Mar 05 '14 at 22:47
  • Couldn't agree more. Cool avatar picture. – this Mar 05 '14 at 22:48
  • `scanf("%c",...)` is a correct, standard method to read a character. `fflush(stdin)` is non-standard and may crash the program. I don't know why you attempt to draw some sort of conclusion about the validity of the latter , based on the former. – M.M Mar 06 '14 at 00:25
  • To actually answer the question about EOF, if `getchar()` returns EOF then it will fail to compare equal to `\n`, so the loop will never terminate. – M.M Mar 06 '14 at 00:26
  • @MattMcNabb `fflush( stdin )` won't lead into any crash, provided that you are using MSVC or possibly some other compilers as well. In case it leads into a problem, one may always use the alternative method provided. – Utkan Gezer Mar 06 '14 at 22:14
  • and what if you aren't using MSVC or possibly some other compilers? Just use the standard method the first time, then we don't have to worry about what compiler is being used. – M.M Mar 06 '14 at 22:16
  • @MattMcNabb The answer of that question is already there, making that question is absolutely redundant. – Utkan Gezer Mar 06 '14 at 22:19
  • @MattMcNabb ... as for the `EOF` check, I was unable to find out even a single example case where extending the `while` condition to make a check for `EOF` does any help. – Utkan Gezer Mar 06 '14 at 22:22
  • Type some characters then press Ctrl-D (or Ctrl-Z if you're in Windows) instead of pressing Enter. – M.M Mar 06 '14 at 22:27
  • @MattMcNabb `EOF` check doesn't do any help about it, or any bad. With or without; I need one further enter key-press to advance out of the `while`, when I append my lines with `CTRL + Z`. – Utkan Gezer Mar 06 '14 at 22:33
  • Something's wrong with your terminal if pressing keys after EOF makes any difference – M.M Mar 06 '14 at 22:44
  • @MattMcNabb This answer says *no* to that claim of yours: http://stackoverflow.com/a/7373799/2736228 – Utkan Gezer Mar 06 '14 at 22:51
  • The "accepted" answer to that question explains , if you press Ctrl-Z but it wasn't at the start of the line, then EOF is not sent (just a buffer flush). So, type some characters and then press Ctrl-Z twice. – M.M Mar 07 '14 at 00:49
  • @MattMcNabb Why in quotes... With the `EOF` check, I type `asd` then; **(a)** `Ctrl + Z` then `Ctrl + Z` then press Enter key 2 times, **(b)** `Ctrl + Z` then Enter once then `Ctrl + Z` again and Enter once again to break out of the `while`. Without the `EOF` check, `asd` then; **(c)** same as the **a**, **(d)** I press the enter key once more at the end to break out of the `while`. So -at best- the difference it makes is just one more Enter-key key-press. Is this it? Is this really the reason for all this? – Utkan Gezer Mar 07 '14 at 07:20
0

You can do it right, or you can do it wrong and hope that the person doesn't enter the right series of keystrokes or pipe in a file that doesn't end in newline, to break it ... up to you

M.M
  • 138,810
  • 21
  • 208
  • 365