Thread: Lower case caracters

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    29

    Lower case caracters

    Hello.

    How can I take a substring of lower case caracters of a String.

    Examples:

    If i have : COmpuTER
    Output: mpu

    If i have : ComputER
    Output : omput

    Sorry, about my english.
    Thanks.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Loop through all the characters of the source string. In the loop, use islower to test whether the character is lowercase; if so, copy the character from the source string to the destination string. Remember to null terminate the destination string after the loop exits.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    What would you do in the case of 2 substrings like "ComPUteR"? Would you merge the two or ignore one of them?
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    Oct 2004
    Posts
    29
    The two.

    But i had already did it.
    Thanks.

    But i have a problem!
    The output is printting other strange characters in the place of the non lower case characters.

    Example: COMputeR
    Output : $"!#pute!

    Help me!
    Thanks.

  5. #5
    Registered User
    Join Date
    Oct 2004
    Posts
    29
    Like this:
    COMputER
    Output :?? put???n @

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    It is far easier for others to help you debug your code if you actually post it (preferably within code tags: [code][/code]).

    From the output, my guess is that you are using the source index in the destination string and an uninitialized destination string. Use a separate index for the destination string, increment it after writing a lowercase character.
    Last edited by Dave_Sinkula; 10-15-2004 at 09:07 AM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User
    Join Date
    Oct 2004
    Posts
    29
    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    main() {
    
      char input[50];
      char new[50];
      int i = 0;
      int j = 0;
    
      fgets(input,50,stdin);
    
      for(i = 0;i<strlen(input);i++) {
    
          if(islower(input[i])) {
    
    	new[i] = input[i];
          
          }
        }
        printf("%s\n", new);
    }

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <string.h>
    
    int main(void)
    {
       char input[50];
       char new[50];
       int i = 0;
       int j = 0;
    
       fgets(input,50,stdin);
    
       for ( i = 0;i<strlen(input);i++ )
       {
          if ( islower(input[i]) )
          {
             new[j++] = input[i];
          }
       }
       new[j] = '\0';
       printf("%s\n", new);
       return 0;
    }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #9
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Having the separate j counter is the right approach, but you don't actually use it anywhere.
    If you understand what you're doing, you're not learning anything.

  10. #10
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    you have to use two counters because if you only used one take the example
    Code:
    JOhn
    the counter goes to 2(0 then 1 then 2) so then it puts the lower case letter in new[2] right? What is before the 2 the 0th and the 1st elements of the array(string might be easier to understand) The answer is random garbage. Then you have to end the string so that C knows it is finished. that is why he put a '\0' at the last element of his array(string). Understand?

  11. #11
    Registered User
    Join Date
    Oct 2004
    Posts
    29
    I have changed the if statement to give the lower case sequence that begin with a lower case vowels:

    char vowels[] = "aeiou";
    /*I do a cycle to verify vowels*/
    if(islower(input[i]) && (input[i] == vowels[j])) {

    Actually i want to do a program that reads a string.
    Then i want after a sequence of lower case sequence beginning with a lower case vowel, insert a '&';

    Example: MargtRTY45efghtAQrtyP
    Output : Margt&RTY45efght&AQrtyP

    Separating the lower case sequence beginning with a lower case vowel.
    Then I make some kind of "APPEND", putting the '&' in the end of the substrings.
    Then CONCAT and.......
    And return again that string formatted.
    I dont know if is possible to do this by this process.

  12. #12
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    When this came up last time, I thought your question had been answered. During that thread, I had cobbled up this (except for the change from # to & and your latest example text).
    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    char *foo(char *dst, const char *src)
    {
       static const char vowel[] = "aeiou";
       int i, j, k = 0, seq = 0;
       for ( i = 0; src[i]; ++i )
       {
          if ( islower(src[i]) )
          {
             for ( j = 0; vowel[j]; ++j )
             {
                if ( src[i] == vowel[j] )
                {
                   seq = 1;
                   break;
                }
             }
          }
          else
          {
             if ( seq )
             {
                dst[k++] = '&';
             }
             seq = 0;
          }
          dst[k++] = src[i];
       }
       if ( seq )
       {
          dst[k++] = '&';
       }
       dst[k] = '\0';
       return dst;
    }
    
    int main()
    {
       char text[] = "MargtRTY45efghtAQrtyP";
       char result [ 2 * sizeof text - 1 ];
       printf("text   = \"%s\"\n", text);
       printf("result = \"%s\"\n", foo(result, text));
       return 0;
    }
    
    /* my output
    text   = "MargtRTY45efghtAQrtyP"
    result = "Margt&RTY45efght&AQrtyP"
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  13. #13
    Registered User
    Join Date
    Oct 2004
    Posts
    29
    But in the last time the special character always appeared at the front of the vowel.
    Now i will try to do my code lokking sometimes to your code!

    Thanks very much.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. Keypress reading
    By geek@02 in forum Windows Programming
    Replies: 1
    Last Post: 06-16-2004, 12:16 PM
  3. Xmas competitions
    By Salem in forum Contests Board
    Replies: 88
    Last Post: 01-03-2004, 02:08 PM
  4. Basic Data types continue
    By viryonia in forum C Programming
    Replies: 6
    Last Post: 03-10-2003, 10:21 AM
  5. opengl program as win API menu item
    By SAMSAM in forum Game Programming
    Replies: 1
    Last Post: 03-03-2003, 07:48 PM