Results 1 to 12 of 12

Thread: Breaking a String Chain

  1. #1

    Thread Starter
    Frenzied Member Tec-Nico's Avatar
    Join Date
    Jun 2002
    Location
    México
    Posts
    1,192

    Breaking a String Chain

    Excuse me.. How can I get a string chain and then beak it up to check letter by letter?

    Here you have a sample of my code:


    Code:
    #include <stdio.h>
    #include <iostream.h>
    #include <string.h>
    #include <stdlib.h>
    
    main()
    {
     
    /* int CharSize;
     int i;
     
     CharSize = 3;
     
     char CadChar[3];
     int Estado;*/
    
     int CharSize;
     int i;
     int Estado;
     
     char *CadChar;
     
     cout<<"Automata Finito"<<endl<<endl;
     
     cout<<"Dame una palabra: ";
     cin>>CadChar;
     cout<<endl;
    
     cout<<CadChar;
    
     CharSize = strlen(CadChar);
     
    /* for(i=1; i <= CharSize; i++){
      cout<<"Dame una letra: ";
      cin>>CadChar[i];
      cout<<endl;
     }*/
     
     for(i=0; i < CharSize; i++){
     cout<<CadChar[i];
     }
    
     Estado = 0;
     
     for(i=0; i < CharSize; i++){
       if(CadChar[i] == 'a'){
       Estado = 0;
      }
      else{
       if(CadChar[i] == 'b' && Estado < 2){
        Estado++;
       }
       
      }
     }
     
     if(Estado == 2){
      cout<<"La cadena es valida"<<endl;
     }
     else{
      cout<<"La cadena es invalida"<<endl;
     }
     
     char a;
     cout<<endl<<endl<<"Presione cualquier tecla para terminar...";
     cin>>a;
     return 0;
    }
    What am I doing wrong?
    We miss you, friend... Rest in Peace, we will take care of the rest of it.

    [vbcode]
    On Error Me.Fault = False
    [/vbcode]
    - Silence is the human way to share ignorance
    Tec-Nico

  2. #2
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    First of all:
    Code:
    #include <stdio.h>
    #include <iostream.h>
    #include <string.h>
    #include <stdlib.h>

    You're using deprecated headers, this is a common mistake by c++ beginners... always use the standard headers, they're defined without the .h extension.

    Here's some code that uses a basic string (defined in <string>) and uses the at function to show the string letter by letter.. you can test for certain values too ofcourse.. this should get you started

    Code:
    #include <iostream> 
    #include <string>
    using namespace std;
    
    int main()
    {
    	string sIn;
    	int iLen;
    		cout << "Please insert a string: " << endl;
    		cin >> sIn;
    	iLen = sIn.length();
    
    	for (int x=0; x<iLen; x++){
    		cout << sIn.at(x) << endl;
                    //you can test things here
    	}
    	
    return 0;
    }
    Last edited by Jop; Feb 27th, 2003 at 03:42 PM.
    Jop - validweb.nl

    Alcohol doesn't solve any problems, but then again, neither does milk.

  3. #3

    Thread Starter
    Frenzied Member Tec-Nico's Avatar
    Join Date
    Jun 2002
    Location
    México
    Posts
    1,192
    Thank you!

    I am going to try it, though I found another way to solve it... And about the headers, I just copied my File from Turbo C... That is why I had so much trouble.. Oh well.. Thanks anyway!
    We miss you, friend... Rest in Peace, we will take care of the rest of it.

    [vbcode]
    On Error Me.Fault = False
    [/vbcode]
    - Silence is the human way to share ignorance
    Tec-Nico

  4. #4
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    Well it's valid for C applications, but for C++ it's recommend to use the new/standard header files...

    and what's the other way you found? Always good to see alternatives
    Jop - validweb.nl

    Alcohol doesn't solve any problems, but then again, neither does milk.

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    No, it's not valid for C applications, the iostream.h header declares some classes.

    It's simply old C++. New C++ uses the headers without .h.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    The other way could be use of the index operator, iterating with pointers through the return value of c_str(), iterating with iterators through the string - there are numerous ways.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  7. #7

    Thread Starter
    Frenzied Member Tec-Nico's Avatar
    Join Date
    Jun 2002
    Location
    México
    Posts
    1,192

    The other way...

    Corned bee, you are right. It is old C++. Those headers are added like that in Dev-C.
    I came with a way to solve what I asked, but I like more the one you proposed, Jop.

    I am used to programming on VB and not on C++, but I used to program on C++ too, so it is just a matter of time to return to my old rhythm. Anyway, the logic used is the same in almost all languages (do not include small talk, it is an exception since it is purely OO)

    Well, here is my solution:


    Code:
    #include <stdio> 
    #include <iostream>
    #include <string>
    
    main()
    {
    int CharSize;
    int i;
    
    char CadChar[100]; //This would limit the word to 100 characters
    
      cout<<"Please give me a word: ";
      cin>>CadChar;
      cout<<endl; 
    
      CharSize = strlen(CadChar);
    
      for(i=0; i < CharSize; i++){
       cout<<CadChar[i]<<endl;
      }
    
    return 0;
    }
    But as you can see the string is limited to 100 characters. In this case, I don't think anyone would type a word that would be longer than 100 characters. Anyway, it is a limitation... By the way, Isn't there a tag like "VCCode", soo it would color the code as VBCode?

    Thank you very much

    PS. Jop, what is this line for?
    using namespace std;
    Is it a comment? I don't think VC++ compilator would admit such a code line... I haven't tried to compile it, though.

    PS2. Corned Bee, that way you said is what I was trying to do but I couldn't achieve it. I could do it on Turbo C... But not on VC++, how would you do it on VC++?
    Last edited by Tec-Nico; Mar 1st, 2003 at 05:48 PM.
    We miss you, friend... Rest in Peace, we will take care of the rest of it.

    [vbcode]
    On Error Me.Fault = False
    [/vbcode]
    - Silence is the human way to share ignorance
    Tec-Nico

  8. #8
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    The user could type in a 100+ word intentionally to make the pc execute some random code for him. It's called buffer overflow

    The
    using namespace std;
    is a valid C++ code line. Read up on namespaces: search this forum for posts by me with the word namespace - I've posted a long explanation a while ago.

    The ways I proposed are all possible with VC++. I willl not do it now however.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  9. #9

    Thread Starter
    Frenzied Member Tec-Nico's Avatar
    Join Date
    Jun 2002
    Location
    México
    Posts
    1,192
    Thanks for the info Corned Bee... But I searched for that word and I found 22 pages...

    Tell me.. which of the subjects of all those 22 pages is the one in which you explained it?
    We miss you, friend... Rest in Peace, we will take care of the rest of it.

    [vbcode]
    On Error Me.Fault = False
    [/vbcode]
    - Silence is the human way to share ignorance
    Tec-Nico

  10. #10
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    My search turned up 7 threads. keyword "namespaces", searching for my exact username only in the C++ forum.

    Anyway, here's the thread.
    http://www.vbforums.com/showthread.p...ght=namespaces
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  11. #11
    Fanatic Member McCain's Avatar
    Join Date
    Jan 2002
    Location
    Sweden/Denmark
    Posts
    802
    That's a good post about namespaces, thank you!
    There's just one thing I think should be changed...
    This means that you have more than one function or global variable with different names, in different modules
    Shouldn't it be "the same names" ?
    Never argue with fools, they will only drag you down to their level, and beat you with experience.

    Q: How do you tell an experienced hacker from a novice?
    A: The latter thinks there's 1000 bytes in a kilobyte, while the former is sure there's 1024 meters in a kilometer

  12. #12
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Yep, thanks.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width