Need some help on a char swap

This is a discussion on Need some help on a char swap within the C Programming forums, part of the General Programming Boards category; Hello all, This is my last hmwk problem, and I'm a little confused with what to do with it. -I ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    8

    Need some help on a char swap

    Hello all,

    This is my last hmwk problem, and I'm a little confused with what to do with it.

    -I need to write a recursive function that changes all occurences of character t in s (string) to character r (replace).

    -For example: if s (string) were "priority", t=='i', and r=='y', s would become "pryoryty".

    (I am asking the user for an input string, t, and r.)

    Here is what I have so far. I understand that this function needs to be recursive, but I was trying to just get it working 1st. If anyone can tell me what is wrong, or what I need to adjust please let me know.

    Also, anyone who can guide me in transforming the function into a recursive one would be a major help. Recursion is making my head hurt since it was thrown at us in the last half of our most recent lecture. Thanks guys.

    Code:
    #include<stdio.h>
    #include<string.h>
    #define SIZE 15
    
    void replace_r(char *s, char c, char r)
    {
    	char *p;
    
    	for (p=s; *p; p++) {
    		if (*p==c){
    			*p = r;
    		}
    	}
    }
       
    main()
    {
    	char original_str[SIZE], s_char[1], r_char[1];																								
    	
    	printf("Original word:");															
    	gets(original_str);														
    	
    	printf("c(original character)=:");													
    	gets(s_char); 																		
    	
    	printf("r(replace)=:");																
    	gets(r_char);																		
    
    	replace_r(original_str, s_char, r_char);
    			
    	printf("New Sentence: %s\n", original_str);					
    }

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,752
    Try reading the site's lesson on recursion and see if studying from a different source helps you convert your current solution into a recursive one. Give it a try. If it breaks, post here, and I will help you fix it.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,761
    Wow, your use of gets() is even more broken than normal. The ONLY input which doesn't cause a buffer overflow is to just press enter. Anything else and you're hosed.

    Read the FAQ and learn to use fgets()
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    478
    1) You're using gets. Gets is bad for buffer overflows. Besides, if you enter a character and then enter using gets and your current variables, you'll have a buffer overflow.

    2) Why use an 'array of size 1' ? Why not char?

    Everything else seems alright.

    Input the word with fgets and the characters with scanf, and you'll be fine.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    8
    Quote Originally Posted by citizen View Post
    Try reading the site's lesson on recursion and see if studying from a different source helps you convert your current solution into a recursive one. Give it a try. If it breaks, post here, and I will help you fix it.
    I will definately do that, thank you for linking that to me. If I have probs I will post them later today, thx guys. This board seriously clears minor probs up for me, and that makes a world of difference to a new programmer.

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    8
    Ok, I did not convert this into a recursive funtion yet, mainly because I am running into an odd problem... Here is my new code, and below that is what is printing out.

    Code:
    #include<stdio.h>
    #include<string.h>
    #define SIZE 15
    
    void replace_r(char *s, char c, char r)
    {
    	char *p;
    
    	for (p=s; *p; p++) {
    		if (*p==c){
    			*p = r;
    		}
    	}
    }
       
    main()
    {
    	char original_str[SIZE], s_char, r_char;																								
    	
    	printf("Original word:");															
    	fgets(original_str, SIZE, stdin);														
    	
    	printf("c(original character):");													
    	scanf("&#37;c", &s_char); 																		
    	
    	printf("r(replace character with):");																
    	scanf("%c", &r_char);																		
    
    	replace_r(original_str, s_char, r_char);
    			
    	printf("New Sentence: %s", original_str);					
    }
    With this new code, I am receiving this result :

    Code:
    Original word:Hello
    c(original character):H
    r(replace character with):New Sentence:
    ello
    It looks like it wants to work, it makes sense that the H was removed... but why it won't let me enter my replace char is beyond me... I THOUGHT my replace function was correct.... any thoughts or fixes guys? thx.


    **EDIT**

    I figured out what the problem was... I needed to insert fflush(stdin); .... code now works and I just need to transform it into a recursive function.
    Last edited by cwafavre; 11-05-2007 at 04:42 PM. Reason: Figured out what was wrong

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,761
    Use fgets() to real ALL your input, then extract the information you need from the buffer into your actual variables.

    Mixing fgets() and scanf() is always a disaster.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User
    Join Date
    Oct 2007
    Posts
    8
    Ok, I looked over the recursive exmples but noticed they were for C++.... little bit different I assume. Can anyone show me or give an example of how I can change my replace function into a recursive function? I am messing around with it, but it's making little sense and coming out horribly wrong. Thx

  9. #9
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,752
    > Ok, I looked over the recursive exmples but noticed they were for C++.... little bit different I assume.

    Not really, but the same tutorial was written with C examples:
    http://www.cprogramming.com/tutorial/c/lesson16.html

    The tutorial teaches two things very well: first, that recursion is a programming technique where the programmer defines the operations of an algorithm in terms of themselves, and second, that base cases control the execution of a recursive algorithm. Base cases are a decision whether to call the function again or not.

    So to make a recursive solution, look at the algorithm for base cases:
    1. Compare the current character with the search character.
    2. If they match, replace the current character with the replacement character.
    3. Repeat step one and 2 until you reach the end of the search string.

    Now that I've defined recursion and outlined the steps of the algorithm, hopefully you can come up with a solution.

  10. #10
    Registered User
    Join Date
    Oct 2007
    Posts
    8
    Ok, I will go back and look over the examples some more. Thank you for taking the time to point those out, I do appreciate it. Now let's see if I can get anywhere with it, thx again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 02:01 AM
  2. Sorting Linked Lists
    By DKING89 in forum C Programming
    Replies: 6
    Last Post: 04-09-2008, 08:36 AM
  3. Conversion Char To Char * Problem
    By ltanusaputra in forum Windows Programming
    Replies: 3
    Last Post: 03-01-2008, 02:06 PM
  4. code condensing
    By bcianfrocca in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2005, 10:22 AM
  5. Extra printed stmts...why?
    By mangoz in forum C Programming
    Replies: 4
    Last Post: 12-19-2001, 07:56 AM

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