Link to home
Start Free TrialLog in
Avatar of john2789
john2789

asked on

C characters and strings

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>


int main()
{
      char *LineOfText;
      char *StringToSearch;
      char *SearchPtr;
      clrscr();
      printf("\n Enter a line of text :");
      gets(LineOfText);
      printf("Enter the search string :");
      gets(StringToSearch);
      for(int i=0;strlen(LineOfText)>0;i++)
      {
            SearchPtr = strstr(LineOfText,StringToSearch);
            printf("SearchPtr = %s\n",SearchPtr);
            sprintf(LineOfText,SearchPtr);
            printf("Line      = %s\n",LineOfText);
      }
      return 0;
}

I made this code, but it didn't work well. Could you tell me which codes are wrong and how I can fix them?

Thanks
Avatar of Dexstar
Dexstar

john2789:

Well, what do you want it to do?

Try removing this line, for starters:
     for(int i=0;strlen(LineOfText)>0;i++)

Hope that helps,
Dex*
Avatar of john2789

ASKER

I don't need it?
Your first bug is that you are reading a string into LineOfText and *StringToSearch without having allocated any memory.
change
     char *LineOfText;
     char *StringToSearch;

to
     char LineOfText[1024];
     char StringToSearch[1024];

This will allow you to use strings that are up to 1023 bytes long
john2789:

> I don't need it?

You don't seem to need it, but you should tell me WHAT the program is SUPPOSED to do, so I can help you make it do that.  Otherwise, I can't really help you.

But guitardude101's suggestions are also valid.  Be sure to incorporate them.

Dex*
Original problem is like this:
write a program that inputs a line of text and a search string from the keyboard. Using function strstr, locate the first occurence of the search string in the line of text, and assign the location to variable searchptr of type char*. If the search string is found, print the reminder of the line of text beginning with the search string. Then, use strstr again to locate the next occurrence of the search string in the line of text. If a second occurrence is found, print the remainder of the line of text beginning with the second occurrence.
TO: guitardude101

hmm... I still have some error... I changed the code you suggested me, but still I have the error. Could you please tell me how I can do?
This is clearly a homework problem.  If you didn't already have a good start on it, I wouldn't help you.  It's against the site rules.

Try this:

      #include <stdio.h>
      #include <conio.h>
      #include <stdlib.h>
      #include <string.h>


      int main()
      {
            char LineOfText[1024];
            char StringToSearch[1024];
            char *SearchPtr;

            clrscr();
            printf("\n Enter a line of text :");
            gets(LineOfText);
            printf("Enter the search string :");
            gets(StringToSearch);
            for ( SearchPtr = strstr(LineOfText, StringToSearch) ;
                        SearchPtr != NULL ;
                        SearchPtr = strstr(SearchPtr+1, StringToSearch) )
            {
                  printf("SearchPtr = %s\n",SearchPtr);
            }
            return 0;
      }

HTH,
Dex*

Boo!
TO: Dexstar

This is not an assignment. I do not goto school right now. I am in home and study C programming by myself. I just picked one problem and have tried to solve problems, and try to understand how C programming will work.


          printf("\n Enter a line of text :");
          fflush (stdin);
          gets(LineOfText);
          printf("Enter the search string :");
          fflush(stdin);
          gets(StringToSearch);
         

           getch();
           return 0;
Do I need fflush(stdin); after printf and gectch() before return 0? Because some of C programming reference books write fflush(..) and gech(). What are these codes and am I better to use these codes for this problem, too?

Thanks
ASKER CERTIFIED SOLUTION
Avatar of Dexstar
Dexstar

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ok, I will try now
no assisist??
TO:guitardude101
how can I put the score on you, too?
hi