Thread: Replacing Characters of a string with asterix.

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    2

    Replacing Characters of a string with asterix.

    Hello everyone,

    i am relatively new to c programming, even though i have used a pc for years now and i can fix almost every issue with the normal windows pc i am very new to C programming, I lack the general logic.

    Anyways i am stuck with a small problem which is say suppose i have a String like Hello, how can i hide few characters of it behind asterix?
    Example String is Hello, i want the output to be like H*ell* or H**llo or what ever way.

    So far i am just stuck with this:


    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    char s[20];
    clrscr();
    printf("\nEnter string : ");
    gets(s);

    then i am lost about the logic please help

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    SourceForge.net: Void main - cpwiki
    SourceForge.net: Gets - cpwiki

    Some intermediate exercises.
    1. Print each char of string s individually, using a for loop and array subscripts.
    2. As 1, but replace vowels with a *
    3. As 2, but replace random letters with a *
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by eastgod View Post
    Example String is Hello, i want the output to be like H*ell* or H**llo or what ever way.
    First is to nail down the requirements. If the string is "Hello", which characters should be replaced by asterisks? Then, do you want to modify the string, so that the string which formerly contained "Hello" will now contain "H**llo" or do you just want to print the string "Hello" but replacing certain characters in the output with "*"?

    If you want to modify the string, here is a simple way:

    s[1] = '*';
    s[2] = '*';

    Then if s contains "Hello", after those lines, it will contain "H**llo". Of course, this assumes that s has a strlen of at least 3 characters.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Welcome to the forum, eastgod!

    Eastgod, you'll find we are REAL sticklers for:

    1) using int main() in your program. Void main has been non-standard since dinosaurs roamed the earth.

    2) using a good (you might say "godly"), indentation style. SO MANY errors are caught immediately, IF (and only if), the code has a good indentation style - where subordinate lines of code, are indented 2-5 char's.

    3) always use [code] tags around your code. Otherwise the forum squishes it all out of shape, to save space and improve appearance.

    Code tags are on an icon button, in the "Go Advanced" editor only. Highlight your code, click on the code tag icon, and poof! You're done.

    If you want to replace a letter, that's very easy to do. If you want to ADD a * to a word, you first have to make sure that you have the room for the extra char you're adding.

    Because most strings will have space for 0 extra char's to be added to them. C doesn't waste space like that.

    Your example changes hello (5 chars), into h*ello (6 chars), so be careful.

    One simple way to do this is to iterate over each letter, and if it's the letter you've chosen to change, change it.

    Code:
    int i;
    char word[6]={"hello"}; //5 letters, and one space for the end of string char: '\0'
    char chosenLetter='l';  //change el's
    
    for(i=0;word[i];i++) {
       if(word[i]==chosenLetter)
          word[i]='*';
    }
    This is not the only way to do this task, of course.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. replacing characters in a file [linux command line]
    By bos1234 in forum C Programming
    Replies: 6
    Last Post: 04-10-2012, 11:23 AM
  2. replacing charcters for other characters
    By terriuchiha in forum C++ Programming
    Replies: 2
    Last Post: 12-10-2010, 09:56 AM
  3. Replacing Characters
    By carrotcake1029 in forum C Programming
    Replies: 3
    Last Post: 04-28-2008, 01:08 PM
  4. Replacing characters
    By Beowolf in forum C++ Programming
    Replies: 8
    Last Post: 11-13-2007, 06:17 PM