Thread: inserting charcters in a string

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

    inserting charcters in a string

    hello, i want to know how to put a '#' after a lower-case character sequence with the first letter being a vogal

    this is what i did:
    Code:
    int i=0;
    int j=0;
    int vogals[]="aeiou";
    for(i=0; i<strlen(input); i++) {
    	for(j=0; j<strlen(vogals); j++) {
    		if(input[i] == vogals[j]){
    			input[i] = '#';
    }
    }
    }
    this is what porogram do:

    abcd245ofg -> #bcd245#fg

    i also can do this: ->a#cd245o#g with input[i+1]='#'

    the program should do this: ->abcd#245ofg#

    I've tried lots of stuff but i coulnd't do it

    if somebody can help i will apreciate

    thanks, Calavera

  2. #2
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    Use code tags when posting and then will talk!

  3. #3
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595
    I am posting this because you did not use code tags on this thread. In the furture please use Code Tags. They make your code MUCH easier to read and people will be much more likely to help you if you do. And they'll be happier about helping you

    For example:

    Without code tags:

    for(int i=0;i<5;i++)
    {
    cout << "No code tags are bad";
    }

    With Code Tags:
    Code:
    for(int i=0;i<5;i++)
    {
         cout << "This code is easy to read";
    }
    This is of course a basic example...more complicated code is even easier to read with code tags than without.

    I've added code tags for you this time. They can be added by putting [code] at the beginning of your code and [/code] at the end. More information on code tags may be found on the code tag post at the top of every forum. I also suggest you take a look at the board guildlines if you have not done so already.

    This is a common first post mistake, just remember to use [code] tags in the future and you'll get much more help.

    If this is your first time posting here the welcome, and if there's anything I can do or any questions I can answer about these forums, or anything else, please feel free and welcome to PM me.


    Good Luck with your program,

    Kermi3
    Lead Moderator
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  4. #4
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    By vogal, I guess you mean vowel. All you have to do is look for a vowel, when you find one, go until you reach an uppercase letter or apparently a number (I gathered that through your examples). Once you reach the termination of your walk through the string, replace the letters, assuming they are allocated and not literals. Look into ctype.h.
    Last edited by chrismiceli; 10-08-2004 at 08:23 AM.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    change this

    Code:
    int vogals[]="aeiou";
    to this

    Code:
    char vogals[]="aeiou";
    Didn't you get some kind of compiler message that would have made you look at this?

    Regards,

    Dave

  6. #6
    I like code Rouss's Avatar
    Join Date
    Apr 2004
    Posts
    131
    You might want to declare a new string and just add the characters you want to that... There might be an easier way, but if there is, I can't think of it.
    Code:
    /* also assuming everything else is declared and initialized */
    char ans[LENGTH]   /* whatever the length you used is */
    int len = strlen(input);
    int go = 0, k = 0;
    for(i = 0; i < len; i++)
    {
      if(!go)
      {
        for(j = 0; j < 5 && !go; j++)
        {  
           if(input[i] == j[i])
           {
              ans[k++] = input[i];
              go = 1;
           }
        }
        if(!go)
        {
           ans[k++] = input[i];
        }
      }
      else  /* go = 1 */
      {
         if(isalpha(input[i]) && islower(input[i])
           answer[k++]=input[i];
         else
         {
            answer[k++] = '#';
            answer[k++] = input[i];
            go = 0;
         }
      }
    }
    /* edited here, also add the \0 to the end */
    ans[k++] = '\0';
    So basically, if go is 0, check for a vowel. If you find a vowel, add it to the answer string, set go to 1. If you don't find a vowel, just add the char to the ans string.
    If go is 1, check for lower case alphas. If you find one, add it to the ans string. If you find something else, add a # and then add the character to the string, and set go to 0 (so you'll start checking for new vowels).

    It could probably be done a lot simplier... I don't have a compiler handy right now, so I can't test the code... Hopefully I didn't screw something up....

  7. #7
    Registered User
    Join Date
    Nov 2003
    Posts
    28
    Here is another way to do this

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    
    main(int argc, char *argv[])
    {
        char input[] = "abcdefghijklmnopqrstuvwxyz";
        char test[] = "aeuio";
        char newname[27];
        
        int x,y,v;
        int atvowel;
        v = 0;
        
        for(x=0;x<sizeof(input);x++){
            for(y=0;y<sizeof(test);y++){
                    
                if(input[x] == test[y]){
                    atvowel = 1;
                    break; //no need to keep checking vowel is found
                }        
            }
            
            newname[v++] = input[x];
            
            if(atvowel == 1 && islower(input[x])){
                newname[v++] = '#';
                atvowel = 0;
            }    
        }
        
        printf("%s\n",newname);
        system("pause");
    }
    Last edited by Vertex34; 10-08-2004 at 09:07 PM.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Vertex34
    Here is another way to do this
    Another way to do it would be to actually pay attention to your compiler's warnings. If it doesn't give you any for the code you've posted, switch them on.

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

  9. #9
    Registered User
    Join Date
    Nov 2003
    Posts
    28
    Quote Originally Posted by quzah
    Another way to do it would be to actually pay attention to your compiler's warnings. If it doesn't give you any for the code you've posted, switch them on.

    Quzah.
    Jeez whats your problem, Instead of being a grouch you could have said what the problem was. Anyways I included the wrong header, instead of string.h I should have included ctype.h. I wrote the program on my laptop last night and it worked fine with string.h (is was thinkin that islower() was defined in there) But after I seen your "helpful" post I ran the code on my desktop comp and it gave me the warning for islower() not being defined.

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >But after I seen your "helpful" post I ran the code on my desktop comp and it gave me the warning for islower() not being defined.
    So quzah's post was actually helpful despite your cute little quotations. What's the problem aside from a bruised ego?

    >you could have said what the problem was
    This is a very lazy attitude. The problem was OBVIOUS, so why should anyone need to tell you what it was?
    My best code is written with the delete key.

  11. #11
    Registered User
    Join Date
    Nov 2003
    Posts
    28
    ok my bad not tryin to ........ people off, yes it ws obvious, but still no need to get nasty about it. And bruised ego? Yeah right I don't know enough about programming in c yet to have an ego.

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >but still no need to get nasty about it
    There's a fine line between nasty and direct. For the most part we're direct, but we occasionally cross the line when there's good cause.

    >I don't know enough about programming in c yet to have an ego.
    That's a good stance, but it's a good idea to refrain from answering questions until you're confident that you can meet the minimum requirements for quality help. One of the more annoying things we deal with is having to correct replies that are farther off the mark than the original question.

    IMO, a new helper is a good thing. But only answer questions you're sure about, and run your code through the grinder before you post it here. Our code guantlet is tough. We let a little bit slide, but for the most part the criticism, while constructive, is very harsh. If there's any doubt, make it clear that you aren't sure so that somebody who is sure can either verify or correct. It also shows a lack of ego, and that goes a long way toward avoiding flames.

    Finally, ignore any attitude from a senior member. We've been here a while and most of us have earned the right to be meaner than average. If you look past the critical comments, you'll get to the meat of the answer and find plenty to learn from. If somebody gives you an attitude and they haven't proven themselves, we'll probably call them on it (none too nicely either). If somebody gives you lip and nobody says anything, you were in the wrong and it would be a good idea to quit before you get too far behind.
    My best code is written with the delete key.

  13. #13
    Registered User
    Join Date
    Nov 2003
    Posts
    28
    Ok, thanks for the advice, i hope to be a part of this board for a long time so I am sorry if I was taking it the wrong way, and I will make sure to check over my code thoroughly for errors before i try to help someone.

  14. #14
    Registered User
    Join Date
    Apr 2004
    Posts
    210
    Quote Originally Posted by Prelude
    One of the more annoying things we deal with is having to correct replies that are farther off the mark than the original question.
    This sounds slightly like you are being paid to work through the boards threads. Well, maybe you are, I don't know :-)

    Quote Originally Posted by Prelude
    earned the right to be meaner than average.
    One can earn respect, reputation and ocasionally somebody elses heart, but the right to be mean? I don't think so. The logical consequence would be that one could eventually - of course not in the scope of a webboard - earn the right to hurt or worse.
    I guess I shouldn't think too much about that sentence, but it's somewhat disturbing none the less...

    aw, ok </the_world_is_so_bad>

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Nyda
    This sounds slightly like you are being paid to work through the boards threads. Well, maybe you are, I don't know :-)
    No. Here's what they meant:

    PersonA asks a question.
    PersonB answers the question incorrectly.
    PersonA says thanks.
    PersonC says no, you're wrong. It's this way...

    It's quite obvious the intent of the sentence.


    Quote Originally Posted by Nyda
    One can earn respect, reputation and ocasionally somebody elses heart, but the right to be mean? I don't think so. The logical consequence would be that one could eventually - of course not in the scope of a webboard - earn the right to hurt or worse.
    I guess I shouldn't think too much about that sentence, but it's somewhat disturbing none the less...
    Why not? Judges "earn" the right, by going to school and kissing enough asses to get elected or appointed, the right to kill people or lock them up, or decree they're insane, or... The list goes on. If someone manages to get themselves declared ruler of NationA, even though they didn't win the election, they can now declare that they have the "right" to declare war on a sovereign nation, bombing innocents into oblivion for the sake of oil. Oh but now we're getting side tracked.

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  2. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  3. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM