Thread: Need algorithm for string operation

  1. #1
    Registered User
    Join Date
    Jun 2011
    Location
    Bolpur, India
    Posts
    42

    Need algorithm for string operation

    Please friends suggest me algorithms to do the following jobs......
    1)Program to read a line and delete all occurences of 'the' or 'THE'
    2)Function to reverse the strings stored in the array of pointers:
    Code:
    char *s[]={
                        "To err is human....",
                         "But to really mess things up....",
                         "One need to know C!!"
                   };
    a function xsterrevIstring) should get this job done.....
    I need only algorithm to implement in C.No coding is needed....

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    1) I would write it in terms of strstr(), to find "the", and then remove it by overwriting it with the rest of the string, using memmove(). Both are found in string.h.

    2) Start with a for loop going backwards and figure out what needs to move around to make the string backwards as well. Unless, you are actually willing to make a copy of the string... that's even easier than doing it in place.

    ETA: Also, that pointer array doesn't have any writable memory in it, because string literals aren't writable. Before you try what I said, make sure not to try changing string literals.
    Last edited by whiteflags; 06-18-2011 at 01:13 AM.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by mistu4u View Post
    Please friends suggest me algorithms to do the following jobs......
    1)Program to read a line and delete all occurences of 'the' or 'THE'
    2)Function to reverse the strings stored in the array of pointers:
    Code:
    char *s[]={
                        "To err is human....",
                         "But to really mess things up....",
                         "One need to know C!!"
                   };
    a function xsterrevIstring) should get this job done.....
    I need only algorithm to implement in C.No coding is needed....
    Already done... your example text does not contain either "the" or "THE" ... Some programs are extremely easy to write.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Location
    Bolpur, India
    Posts
    42
    sorry commontater you didn't get it.Both the programs are different.!st program should take the user input and delete the 'THE' or 'the's.In case of the 2nd program the array of pointers is given.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Well, gee I'm sorry but you can search that sample text all you want and it ain't gonna find the or THE anywhere...

    If you want the search function... use strstr() ... If the second program wants to print the pointers just use printf() ...

    This is childishly simple code to write... but your search is going to fail no matter what you do.

  6. #6
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by mistu4u View Post
    sorry commontater you didn't get it.Both the programs are different.!st program should take the user input and delete the 'THE' or 'the's.In case of the 2nd program the array of pointers is given.
    If you weren't such a lazy git, you would have already found a ton of examples on how to do this on any search engine your heart desires.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. recursive string operation..
    By transgalactic2 in forum C Programming
    Replies: 72
    Last Post: 01-09-2009, 04:42 PM
  2. Which string operation is faster? Which is better?
    By 6tr6tr in forum C++ Programming
    Replies: 4
    Last Post: 04-02-2008, 11:15 AM
  3. Interface to an high level string operation
    By pheres in forum C++ Programming
    Replies: 0
    Last Post: 03-27-2008, 08:48 AM
  4. string operation and related exception
    By George2 in forum C++ Programming
    Replies: 63
    Last Post: 03-04-2008, 02:54 AM
  5. Really basic string operation
    By bobthebullet990 in forum C Programming
    Replies: 6
    Last Post: 11-28-2005, 05:18 PM