Thread: Character Replacement in a string

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    11

    Character Replacement in a string

    Hi,
    i would like your help in a program.
    Code:
    #include <stdio.h>
    #include <string.h>
    char s[1000];
    int i,j,r,k,l;
    
    int counter_aA (m)
    
    {
    k=0;
       for(i=0; i<m; i++)
        {
          if (s[i]=='a'||s[i]=='A')
            k++;
    
        }
     return k;
        }
    
    int counter_zZ (m)
    {
     l=0;
    
       for(i=0; i<m; i++)
        {
          if (s[i]=='z'||s[i]=='Z')
            l++;
        }
    
            return l;
    
    }
    
    
    
     int main()
    {
      printf("Enter Text Line and press ENTER :");
      gets(s);
      r=strlen(s);
    
      printf("\n\n\n\nThe text is: ");
      puts(s);
      printf("\nThe number of aA is : %d\n",counter_aA(r));
      printf("The number of zZ is : %d\n",counter_zZ(r));
    
    
      if (k>l)
      {
          for(i=0; i<strlen(s); i++)
        {
    
         if (s[i]=='a')
          s[i]='z';
          if (s[i]=='A')
          s[i]='Z';
        }
    printf("\n\n\n\nThe modified text is: ");
    puts(s);
    printf("\nThe NEW number of aA is : %d\n",counter_aA(r));
    printf("The NEW number of zZ is : %d\n",counter_zZ(r));
    
    }
    else
          if (k<l)
    {
    
        for(i=0; i<strlen(s); i++)
     {
          if (s[i]=='z')
          s[i]='a';
          if (s[i]=='Z')
          s[i]='A';
         }
    printf("\n\n\nThe modified text is: ");
    puts(s);
    printf("\nThe NEW number of aA is : %d\n",counter_aA(r));
    printf("The NEW number of zZ is : %d\n",counter_zZ(r));
          }
          else
          {
     if (k==l)
     {
        for(i=0; i<(strlen(s)/2); i++)
    {
          if (s[i]=='a')
          s[i]='z';
    
          if (s[i]=='A')
          s[i]='A';
    
    }
    j=(strlen(s)-1);
         while(j>(strlen(s)/2))
         {
          if (s[j]=='z')
          s[j]='a';
          else
          if (s[j]=='Z')
          s[j]='A';
          j=j-1;
    }
    printf("\n\n\nThe modified text is: ");
    puts(s);
    printf("\nThe NEW number of aA is : %d\n",counter_aA(r));
    printf("The NEW number of zZ is : %d\n",counter_zZ(r));
    }
    }
    }

    the problem asks to enter a text line an first print it then count both 'a'and'A' and 'z' and 'Z'of that line

    --if the number of 'a'and'A' is > than 'z' and 'Z' replace 'a' with 'z' and 'A'with 'Z'
    print the new line and count the new aA and zZ and vise versa.
    --if the number of 'a'and'A' is equal(=) to 'z' and 'Z', replace 'a' with 'z' and 'A'with 'Z' until the middle of the sentence and from the end of the sentence to the middle(middle char is included) replace the 'z','Z' with 'a','A'.
    Print the new sentence and the new number of 'a','A' and 'z','Z'
    my code seems correct but i m told to use char*gets(char*s) instead of gets() but i don't know how.
    Could you please help me ?
    Thanx in advance...
    Last edited by junglist; 02-15-2011 at 06:10 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    my code seems correct but i m told to use char*gets(char*s) instead of gets() but i don't know how.
    You are using gets correctly - as correct as it ever can be - but, assuming you're compiling with warnings on, you're probably actually being warned against using it. See the FAQ (click!) for more info.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    I suggest starting by fixing your code so it actually compiles without warnings. I suggest cranking the warnings all the way up while you learn to code. Some other tips:

  4. #4
    Registered User
    Join Date
    Feb 2011
    Posts
    11
    Quote Originally Posted by quzah View Post
    You are using gets correctly - as correct as it ever can be - but, assuming you're compiling with warnings on, you're probably actually being warned against using it. See the FAQ (click!) for more info.

    Quzah.
    thnx for the faq quzah...i ll read it.....
    as for the warning i get 0 errors 0 warnings in code::blocks
    .and again thank you...

  5. #5
    Registered User
    Join Date
    Feb 2011
    Posts
    11
    Quote Originally Posted by anduril462 View Post
    I suggest starting by fixing your code so it actually compiles without warnings. I suggest cranking the warnings all the way up while you learn to code. Some other tips:
    i fixed the metrhths_aA it is counter_aA

    is strpbrk() of any use in this kind of programm?

    Thank you all for your answers.I'll try to read the FAQs ASAP...

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by junglist View Post
    is strpbrk() of any use in this kind of programm?
    Yes.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You could use it, but why? You are still going to end up calling it in a loop to get all the characters you want, and it's still going to be running a loop, so it's really more inefficient than just having a single loop of your own.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    True, though I was thinking calling it from the character after the one you just found and replaced.

  9. #9
    Registered User
    Join Date
    Feb 2011
    Posts
    11
    Is there anyway getting a string without declairing the lenght of itat the begining and without dealing with programming problems?
    If so do i have to make many changes in my initial code?
    Thnx in advance

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by junglist View Post
    Is there anyway getting a string without declairing the lenght of itat the begining and without dealing with programming problems?
    You can declare a char pointer and when you know how much space you need, use malloc():

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void) {
    	char *buffer, ch;
    	int sz = 4096, count = 0;
    
    /* allocate initial memory */
    	if (!(buffer = malloc(sz))) {
    		puts("Out of memory!");
    		return 1;
    	}
    
    /* read from stdin */
    	while ((ch = getchar())) {
    		if (feof(stdin)) break;
    		if (count == sz-1) {	/* increase allocated memory as necessary */
    			sz *= 2;
    			if (!(buffer = realloc(buffer,sz))) { 
    				puts("Out of memory!");
    				return 1;
    			}
    		}
    		buffer[count++] = ch;  
    
    	}
    	buffer[count] = '\0';
    
    	printf("BUFFER CONTENTS:\n%s\n",buffer);
    
    	free(buffer);
    	return 0;
    }
    Last edited by MK27; 02-16-2011 at 09:22 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by junglist View Post
    Is there anyway getting a string without declairing the lenght of itat the begining and without dealing with programming problems?
    If so do i have to make many changes in my initial code?
    Thnx in advance
    The practicality, in a language that does not have native support for strings, is that you're going to have to create a character buffer someplace. There are trick you can use such as going character by character and expanding the buffer as you go (per MK27's suggestion) but ultimately you are going to have to provide memory space for it somehow.

    The standard C functions getline() and getdelim() should interest you as both read from an input stream and allocate their own memory for your strings. You may also want to look at strdup() when moving and sorting... However; these functions will require you to provide pointers to strings and to fee the memory when you're done with it.

    There's just no way around manual memory management in C (and that's a good thing).
    Last edited by CommonTater; 02-16-2011 at 08:20 AM.

  12. #12
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by junglist View Post
    as for the warning i get 0 errors 0 warnings in code::blocks
    Learn the name of the Compiler you are using and post the name in threads like this.
    Turn on the option to increase the warning from the Compiler as suggested before.
    Have the C::B display the full command line to help you learn what it is doing.
    How to Set C::B to Full Command line FAQ - CodeBlocks

    Tim S.

  13. #13
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by CommonTater View Post
    The standard C functions getline() and getdelim() should interest you...
    Getline() and getdelim() are not standard functions (they are GNU extensions) but they are useful if you have them.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  14. #14
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by MK27 View Post
    Getline() and getdelim() are not standard functions (they are GNU extensions) but they are useful if you have them.
    Not just GNU... they are described in ISO TR24731-1 and 2, and are implemented in PellesC and others as part of stdio.h ... which is, by gosh, part of the standard library.

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by CommonTater
    they are described in ISO TR24731-1 and 2
    Hence they are not (yet) part of the C standard. In fact, neither appears in WG14 N1256 (PDF), which the C standard committee's website considers the "lastest (latest?) publically available version of the standard" (though of course, it is still not officially the standard).

    Quote Originally Posted by CommonTater
    and are implemented in PellesC and others as part of stdio.h ... which is, by gosh, part of the standard library.
    That does not mean that they are part of the standard library. It does mean that they are extensions to the standard library.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  2. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  3. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  4. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 12:47 PM
  5. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM