Thread: replacing a charecter in a string

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    102

    replacing a charecter in a string

    Hallo, I am trying to replace a character in a file with another. To get access to teh characters in the file I used the fgetc function. I can print out the contents of file on the screen but the problem is I can't get to replace the character. Any help is appreciated. Here is my attempt.

    Code:
    #include <stdio.h>
    
    int main() {
      char c;         
      char string[40]; 
      FILE *file;
      int i=0; 
    
      file = fopen("example.txt", "r"); 
      //Here I am opening the file and the file opens successfully
    
      if(file==0) {
        perror("fopen");
          }
    		//Here is now the loop, seems there is a problem
    	//here though I can't figure out exactly
          while ((c = fgetc(file))!=EOF)
    	{
    	for (i; i<sizeof(c); i++)
    	string[i] = c;
    	if (string[i]=='a')//I want to replace with the character
            //b everywhere a appears and then printout the result
    	{string[i]='b';}
    	printf("%c",c);}//Here is my attempt to print out but the characters
           // are not replaced
    	
    
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You're printing c. You didn't attempt to change c; you attempted to change string. Maybe you should print string instead.

    Also are you aware that the for-loop only ever happens for the first character? sizeof(c) is always one; after the first character is read, i becomes one and the condition on the for-loop is never satisfied.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    102
    Quote Originally Posted by tabstop View Post
    You're printing c. You didn't attempt to change c; you attempted to change string. Maybe you should print string instead.

    Also are you aware that the for-loop only ever happens for the first character? sizeof(c) is always one; after the first character is read, i becomes one and the condition on the for-loop is never satisfied.

    Thank you so much! That helped me figure out where the problem lay. So now I changed c instead of changing the string and I got what I wanted. Thank you once again. Here is the code and it works.

    Code:
    #include <stdio.h>
    
    #include <stdio.h>
    
    int main() {
      char c;         
      char string[40]; 
      FILE *file;
      int i=0; 
    
      file = fopen("example.txt", "r"); 
      //Here I am opening the file and the file opens successfully
    
      if(file==0) {
        perror("fopen");
          }
    		//Here is now the loop, seems there is a problem
    	//here though I can't figure out exactly
          while ((c = fgetc(file))!=EOF)
    	{
    	for (i; i<40; i++)
    	c = string[i];
    	if (c=='a')
    	{c='b';}
    	printf("%c",c);}
    	
    
    }

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    102
    Ok. I tried to print
    Code:
    printf("%s",string)
    but it doesn't work. I only get a series of ssssss and some funny characters. I don't know where I am doing something wrong.
    Last edited by Dontgiveup; 06-03-2009 at 05:16 PM.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    What you have doesn't even compile. Assuming you mean "%s", then look at your for loop. Notice how every time you read a letter, you fill in all the spaces in string with that letter -- including the "end of string" character \0, meaning your string no longer ends.

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    102
    OK then, which is the correct way to printout the string then? Is it by printing out string[i]? I am new to these stuffs and especially c.
    Also before I meant "%s" to print out the string.

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    I think the best way to fix this program is to refine the way you solve the problem. Your string variable isn't strictly necessary considering you will echo every character back. If you simply do what you know how to do in this case you should be fine. Just change c to 'b' when it is 'a', then print it out.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Dontgiveup View Post
    OK then, which is the correct way to printout the string then? Is it by printing out string[i]? I am new to these stuffs and especially c.
    Also before I meant "%s" to print out the string.
    %s is the proper way to print out the string. But since you put garbage in string[], it's going to print out as garbage no matter what you do.

  9. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    115
    Quote Originally Posted by Dontgiveup View Post
    OK then, which is the correct way to printout the string then? Is it by printing out string[i]? I am new to these stuffs and especially c.
    Also before I meant "%s" to print out the string.
    The string "char string[40]" doesn't contain valid things, though, that's why it's not printing out the string that you want.

    Since "char string[ 40 ]" doesn't contain valid things, This code doesn't make sense for me:
    Code:
    for (i; i<40; i++)
    c = string[i];
    All the valid characters are retrieved by "char c":
    Code:
    while ((c = fgetc(file))!=EOF)

  10. #10
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Methinks you need to start all o'er again and clear-up the confusion by stating the issue clearly.

  11. #11
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    You don't need the variable string at all. Remove it completely and the program should work fine.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  2. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM