extracting from a string.

This is a discussion on extracting from a string. within the C Programming forums, part of the General Programming Boards category; Hey guys, I am learning C programming, Can someone present a program to explain this problem: "Write a program that ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    15

    extracting from a string.

    Hey guys, I am learning C programming, Can someone present a program to explain this problem:

    "Write a program that extracts part of the given string from the specified position.
    String
    Code:
    char *str[]={"Working with strings is fun"};
    "

    Suppose I want to extract the word 'fun' and replace it with 'enjoy', How do I do it?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    Write a program that extracts part of the given string from the specified position.
    That means that suppose I gave you the start position as 8 and the end position as 11 (or I told you that I wanted four characters), then you would provide me the substring "with".

    Suppose I want to extract the word 'fun' and replace it with 'enjoy', How do I do it?
    That is a little more challenging. I suggest that you solve your current problem first.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    15
    Sorry I should have mentioned it as two problems. I will be more explicit.
    1. Extract a part of a string by giving the start position and end position.
    2. Search for any word in a string, if found then remove it from the string and replace it with a new word.

    Sorry.

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,714
    Well again, solve your first problem.

    It depends on what is meant by "extract" but good practice for solving problem two, would be to copy the substring from str to interesting_part for problem one, and then physically moving the rest of the string to make room for the new word in problem two.

    When you've created sufficient space, go ahead and overwrite the old word by copying the new one in.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    Ah, I see. So what have you tried for part 1?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    15
    Ain't getting anything. Mam, can you please just solve the first problem for me. Please.

  7. #7
    Registered User
    Join Date
    Jun 2008
    Posts
    4
    PART 1 ref code

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <assert.h>
    
    //extract a aubstring from a given string 'src' here
    //from the position start to end
    //return value: the result substring
    char *Substr( char *src, int start, int end );
    
    int main(int argc, char *argv[])
    {
        char *resstr = NULL;
        char *src = "aakld sdkfjlei ksd";
        
        resstr = Substr( src, 1, 3);
        
        printf("%s",resstr);
            
        system("PAUSE");	
        return 0;
    }
    
    char *Substr( char *src, int start, int end )
    {
         assert( src );
         
         if( start <0 || end >= strlen( src ) )
         {
             perror("Substr():illegal index!");
             return NULL;
         }
         
         char *pstr = src+start;
         char *substr = NULL;
         int index;
         
         substr = (char *) malloc ( (end - start + 1) * sizeof( char ) );
         if(!substr)
         {
                   perror("Substr():allocate memeory failed!");
                   return NULL;
         }
             
         for( index =0; index <= end -start; index ++)
              substr[ index ] = pstr[ index ];
         
         substr[index] = '\0';
         
         return substr;
    }

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,590
    As if by magic, an answer appears on a plate, and the OP has lost any chance of learning anything

    At least it's bad C++, as opposed to good C, so perhaps there is still time to learn.....
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered User
    Join Date
    Nov 2004
    Location
    India
    Posts
    515
    At least it's bad C++, as opposed to good C, so perhaps there is still time to learn.....
    Code:
    string s="this is fun";
    string t="fun";
    string st="nofun";
    s.replace(s.find(t),t.size(),st);


    @OP : Try to solve the problem on your own and post your approach here. Don't expect people to write code for you.
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

  10. #10
    PkD
    PkD is offline
    Registered User
    Join Date
    Aug 2008
    Posts
    3
    now it's compiling!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <assert.h>
    
    //extract a aubstring from a given string 'src' here
    //from the position start to end
    //return value: the result substring
    char *Substr( char *src, int start, int end );
    
    int main(int argc, char *argv[])
    {
        char *resstr = NULL;
        char *src = "aakld sdkfjlei ksd";
        
        resstr = Substr( src, 15, 17);
        
        printf("%s",resstr);
            
        system("PAUSE");	
        return 0;
    }
    
    char *Substr( char *src, int start, int end )
    {
         char *pstr = src+start;
         char *substr = NULL;
         int index;
         
         assert( src );
         
         if( start <0 || end >= strlen( src ) )
         {
             perror("Substr():illegal index!");
             return NULL;
         }
         
         
         
         substr = (char *) malloc ( (end - start + 2) * sizeof( char ) );
         if(!substr)
         {
                   perror("Substr():allocate memeory failed!");
                   return NULL;
         }
             
         for( index =0; index <= end -start; index ++)
              substr[ index ] = pstr[ index ];
         
         substr[index] = '\n';
         index++;
         substr[index] = '\0';     
         
         return substr;
    }

  11. #11
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    1. char *src
    should be
    const char *src
    because it is pointing to the string literal that cannot be changed

    2. This will require to modify the
    char *Substr( char *src, int start, int end );

    to be
    Code:
    char *Substr(const char *src, int start, int end );
    which is more correct because it does not change the src argument

    3. printf("&#37;s",resstr);
    you should check the resstr before printing it, passing NULL pointer to printf will cause the crash

    4. you need to free the allocated memory at the end

    5. Do not cast malloc in C - read FAQ

    6. ANSI C does not allows C++ style comments
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  12. #12
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,590
    Don't forget to add some start > end tests as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 01:45 PM
  5. Again Character Count, Word Count and String Search
    By client in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 11:40 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21