Results 1 to 9 of 9

Thread: String Manipulation

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800

    String Manipulation

    Okay now I have my text from a file. Is there an easy way to search the string other than making some huge function?

  2. #2
    jim mcnamara
    Guest
    in C

    strchr(bigstring, character) looks for a char in a string
    str(bigstring, littlestring) looks for a small string in a big one

    C++

    CString has instr to find substrings.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    what header is needed?

    error C2065: 'str' : undeclared identifier

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    CString is MFC, Jim For normal C++ it's best to use string.

    However, if it's a large file (more than about 250K), it can be a lot faster to use a rope.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  5. #5
    jim mcnamara
    Guest
    oops -

    strstr(bigstring, littlestring);

    my typo.

    If you go into VC++ help and look up strcmp( ) or any other string thing - you have a link to all of the functions as well as CString.

    Parskie is right - but th eonly serious code I've written in C++ was MFC - a while back. I'm back to C at work now.

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    parksie: what is a rope? I never heard of it.
    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
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    I haven't used the STL::rope yet but it's suppose be a string that is good for frequent concentrations of long strings, in contrast to char arrays which would take linear time to concentrate.
    http://www.sgi.com/tech/stl/Rope.html
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    okay this strstr() function is really throwing me off. How do I use it? I just want to know where the little string is in the big string (like vb's InStr).

  9. #9
    jim mcnamara
    Guest
    Code:
    #include <stdio.h>
    
    void main(void){
      char *buf, *s;
      int where;
      char bigstring[100];
      char littlestring[10];
    
      memset(littlestring,'\0',sizeof(littlestring) );/* zero out memory */
      memset(bigstring,'\0',sizeof(bigstring) );
      strcpy(bigstring,"this is a test.");  /* put stuff in test strings */
      strcpy(littlestring,"is");
    
      buf = (char*) strstr(bigstring,littlestring); /* do instr( ) */
      if (buf != NULL) {/* we found one if buf != NULL */
         s = bigstring;
         where = buf - s;
         printf("position = %d\n",where);
      }
      else {
           printf("Not found. \n");
      }
    }

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