Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,150 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,094 people online right now. Registration is fast and FREE... Join Now!




Search for the last occurance of a substring in a string

 
Reply to this topicStart new topic

Search for the last occurance of a substring in a string

vetega
25 Feb, 2007 - 07:42 AM
Post #1

New D.I.C Head
*

Joined: 11 Feb, 2007
Posts: 5


My Contributions
Hi,

I'm trying to write a program that only prints strings that end with "ing". So far I'm not doing a very good job with it.I was wondering if i could get some guidence.
this is my program:

CODE

#include <stdio.h>
#include <string.h>
#include <ctype.h>

void main() {
  char text[100];            
  int str = 'ing';        
                
  
  
  printf("\n Please enter strings: (press return to end)\n");
   gets(text);
  
  while (strlen(text) >= 1){
  
         
  if(strrchr(text,str) != NULL)
    
   printf("The string is : %s\n",text);
        
  
      
     gets(text);
  
  }
  
}


As you can see it will print all the strings that contain 'g'. I've tried to use strchr() but it looks for the firt occurrence not the last.

Thanks
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Search For The Last Occurance Of A Substring In A String
25 Feb, 2007 - 07:45 AM
Post #2

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,226



Thanked: 37 times
Dream Kudos: 25
My Contributions
http://www.cplusplus.com/reference/clibrar...ng/strrchr.html

Looks for last occurrence.

Or, you can judiciously use strstr():
http://www.cplusplus.com/reference/clibrar...ing/strstr.html

You should note that both strrchr() and strchr() take a single character as the needle component, not a string.
User is offlineProfile CardPM
+Quote Post

vetega
RE: Search For The Last Occurance Of A Substring In A String
25 Feb, 2007 - 08:01 AM
Post #3

New D.I.C Head
*

Joined: 11 Feb, 2007
Posts: 5


My Contributions
I can see why strrchr was not working but strstr () finds the first ocurrence. I need the last three charecters of the string. I'm not sure how to do that. I tried to play with the lenght of the string but that didn't work. Any suggestions.

Thanks for your help

User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Search For The Last Occurance Of A Substring In A String
25 Feb, 2007 - 08:51 AM
Post #4

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,226



Thanked: 37 times
Dream Kudos: 25
My Contributions
There are certainly a few ways to do it...Can I safely assume you must use C functionality, and not C++?
User is offlineProfile CardPM
+Quote Post

vetega
RE: Search For The Last Occurance Of A Substring In A String
25 Feb, 2007 - 09:02 AM
Post #5

New D.I.C Head
*

Joined: 11 Feb, 2007
Posts: 5


My Contributions
QUOTE(Amadeus @ 25 Feb, 2007 - 09:51 AM) *

There are certainly a few ways to do it...Can I safely assume you must use C functionality, and not C++?



Yep C functionality

QUOTE(vetega @ 25 Feb, 2007 - 09:59 AM) *

QUOTE(Amadeus @ 25 Feb, 2007 - 09:51 AM) *

There are certainly a few ways to do it...Can I safely assume you must use C functionality, and not C++?



Yep C functionality


I've tried to play around with strstr() but I'm just making a mess of my code. Should I be looking to create an index?

User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Search For The Last Occurance Of A Substring In A String
25 Feb, 2007 - 09:04 AM
Post #6

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,226



Thanked: 37 times
Dream Kudos: 25
My Contributions
One basic way is to use two strings -your original string, and a second string you set up to copy to. Use strstr() to search the original string for the first occurrence. Once found, copy all the characters after that to another string (strncpy()). Search that string for the first occurrence, repeat until you've found the last occurrence.
User is offlineProfile CardPM
+Quote Post

vetega
RE: Search For The Last Occurance Of A Substring In A String
25 Feb, 2007 - 09:37 AM
Post #7

New D.I.C Head
*

Joined: 11 Feb, 2007
Posts: 5


My Contributions
QUOTE(Amadeus @ 25 Feb, 2007 - 10:04 AM) *

One basic way is to use two strings -your original string, and a second string you set up to copy to. Use strstr() to search the original string for the first occurrence. Once found, copy all the characters after that to another string (strncpy()). Search that string for the first occurrence, repeat until you've found the last occurrence.


I'm sorry, i still can see it. How do I check if it has found it at all?
CODE

#include <stdio.h>
#include <string.h>
#include <ctype.h>

void main() {
  char text[100];
  char temp[100];
  char *str;      
                

    
  printf("\n Please enter strings: (press return to end)\n");
   gets(text);
  
  while (strlen(text) >= 1){
  
for (int i =0; i <= text[i];i++){
          
           str = strstr (text,"ing");
          

           strncpy (str,temp,text[i]);

           str = strstr (temp,"ing");
          strncpy (str,text,temp[i]);

}
    
   printf("The string is : %s\n",text);
        
  
      
     gets(text);
  
  
  
}
}

User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Search For The Last Occurance Of A Substring In A String
25 Feb, 2007 - 09:48 AM
Post #8

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,226



Thanked: 37 times
Dream Kudos: 25
My Contributions
You would have to check to see if it's there by checking the retun value of strstr:

http://www.cppreference.com/stdstring/strstr.html

Another method would be to tokenize the string using whitespace as the delimiter, separating it into individual words. By checking each word with strstr, you can find the final occurrence (assuming one word does not have duplicate occurrences of 'ing').

http://www.cppreference.com/stdstring/strtok.html

User is offlineProfile CardPM
+Quote Post

vetega
RE: Search For The Last Occurance Of A Substring In A String
25 Feb, 2007 - 09:56 AM
Post #9

New D.I.C Head
*

Joined: 11 Feb, 2007
Posts: 5


My Contributions
I think I'm going to stick with the first option. I've tried with tokens and I couldn't get it working. This is my last attempt. It compiles, but still doesn't work. Any ideas. Thank you for your help, I'm new at this and it is driving me crazy.
CODE

#include <stdio.h>
#include <string.h>
#include <ctype.h>

void main() {
  char text[100];
  char temp[100];
  char *str;      
                

    
  printf("\n Please enter strings: (press return to end)\n");
   gets(text);
  
  while (strlen(text) >= 1){
  
      for(int i=0; i<= text[i];i++){
          
          if(strstr(text,"ing")==NULL){

              gets(text);
          
          }else{
              
          str = strstr (temp,"ing");
          strncpy (text,temp,i);
          }      
          if(strstr(temp,"ed")==NULL){

               gets(text);

          }else{
              
           str = strstr (temp,"ing");

           strncpy (temp,text,i);
          }
      }
    
          printf("The string is : %s\n",text);
           gets(text);  
  
  
  
}
}

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 11:02PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month