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

5
  • Dangerous code. scanf() will keep scanning and the filling in data into memory until Uth finds a white-space character, but you gave it only a byte of memory. Commented Mar 5, 2014 at 21:16
  • Do not use gets() and scanf()!! Commented Mar 5, 2014 at 21:25
  • I would recommend you use the loop construct like this: while(string[i++] != '\0') { if(string[i] == oldLetter) string[i] = newLetter; } Commented Mar 5, 2014 at 21:27
  • @reza.safiyat That code will go out of bounds. Commented Mar 5, 2014 at 21:28
  • I may be wrong. Could you explain? Commented Mar 5, 2014 at 21:30

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

it most probably still won't
Basically because of the new-line character \n left inside the stream, which will get recognized as the value to be scanned into the newLetter, if not discarded beforehand.
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.

20 Comments

No. fflush( stdin ) is undefined behavior.
@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.
Also while( getchar( ) != '\n' ); should check for != EOF.
Because you don't check for EOF and might miss it, causing an inf loop.
@self. That doesn't explain anything. Explain me why EOF should not be missed when each line is terminated by a '\n'.
|
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.