need help with C-string program

This is a discussion on need help with C-string program within the C++ Programming forums, part of the General Programming Boards category; Can anybody help me with my prgram? I have to be able to replace a character in a string with ...

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    4

    need help with C-string program

    Can anybody help me with my prgram?
    I have to be able to replace a character in a string with another character.
    Example: my string is "Today is monday"
    I want to replace all the a's with w's. So that my string looks like:
    "Todwy is mondwy"
    If anybody can help me with this as soon as poosible I would really appreciate it. Thanks.

  2. #2
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Location
    Mt. Prospect, IL
    Posts
    2,572
    First of all, no one here will do your homework. Second read the board rules. Third post some of your code and you will get plenty of replies...give it a try.


    hints:
    traverse the array and flag anytime the character you want to replace is there and replace is with whatever else you want. To do this you can use a for loop or a while loop with the limit the end of the array; or if the array is longer then the actual string then upto a null symbol

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  3. #3
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,220
    you are only allowed to look if this isn't homework.

    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	int i;
    	char str[] = "Today is monday";
    
    	/* Iterate through each character of the string */
    	for (i = 0; str[i] != '\0'; i++) {
    
    		/* replacing 'a's... */
    		if (str[i] == 'a') {
    
    			/* ...with 'w's */
    			str[i] = 'w';
    		}
    	}
    
    	/* too easy */
    	printf("%s", str);
    	return 0;
    }
    Last edited by Brian; 12-01-2003 at 01:45 PM.

  4. #4
    WDT
    WDT is offline
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271

    Talking

    try:
    Code:
     while(*s){ if (s == 'a') *s = 'w';}
    that should do it or something like that.(It's unchecked so if you get error you may have to use some logic sense

  5. #5
    WDT
    WDT is offline
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271
    Errmm
    Code:
    while(*s){ if (*s == 'a') *s = 'w';}
    (Still unchecked)
    Last edited by WDT; 12-01-2003 at 02:18 PM.

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    You can always use replace defined in the algorithm header.
    Code:
    #include <algorithm>
    #include <string>
    #include <iostream>
    using namespace std;
    
    int main()
    {
        string str("Today is monday");
    
        cout << str << endl;
        replace(str.begin(),str.end(),'a','w');
        cout << str << endl;
    
        return 0;
    }
    Output:
    Today is monday
    Todwy is mondwy
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Originally posted by axon
    First of all, no one here will do your homework.
    Originally posted by Brian
    < potential homework solution >
    Originally posted by WDT
    < potential homework solution >
    Originally posted by hk_mp5kpdw
    < potential homework solution >
    Well, some people might.

  8. #8
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Location
    Mt. Prospect, IL
    Posts
    2,572
    Originally posted by jlou
    Well, some people might.
    yes I've noticed...I wonder if any of the above posters could do my homework, or if they will even post a reply to my programming questions.

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I wonder if any of the above posters could do my homework
    I wonder if any of the above posters would do my work for me so that I can turn it in and do more interesting things.
    My best code is written with the delete key.

  10. #10
    Registered User
    Join Date
    Dec 2003
    Posts
    4
    I was just asking for help, not asking for someone to do i for me. I had an idea about how my prgram should go, but when I complied it, it wasn't replacing the letters. I just wanted some help. So thanks to you all. It did help me see what itw as that I was doing wrong.

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I was just asking for help, not asking for someone to do i for me.
    We are happy to help, but when you ask a question that reeks of homework and fail to supply the offending code, we tend to assume that it is indeed homework and you are looking for an easy out. Just fair warning for the next time.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. Abnormal program termination
    By Kayoss in forum C++ Programming
    Replies: 3
    Last Post: 05-16-2006, 03:29 PM
  4. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 11:47 AM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21