Solved

C characters and strings

Posted on 2003-10-31
13
350 Views
Last Modified: 2012-05-04
#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
0
Comment
Question by:john2789
  • 6
  • 4
  • 2
  • +1
13 Comments
 
LVL 19

Expert Comment

by:Dexstar
ID: 9660953
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*
0
 

Author Comment

by:john2789
ID: 9660956
I don't need it?
0
 
LVL 1

Expert Comment

by:guitardude101
ID: 9660962
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
0
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
LVL 19

Expert Comment

by:Dexstar
ID: 9660982
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*
0
 

Author Comment

by:john2789
ID: 9661129
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.
0
 

Author Comment

by:john2789
ID: 9661149
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?
0
 
LVL 19

Expert Comment

by:Dexstar
ID: 9661187
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!
0
 

Author Comment

by:john2789
ID: 9661329
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
0
 
LVL 19

Accepted Solution

by:
Dexstar earned 125 total points
ID: 9661412
john2789:

> 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.

Okay, that's fair.  And a good way to learn.

> 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?

I don't think you need them.  Try it without them, and see if it works.  gets() should be all you need.

Dex*
0
 

Author Comment

by:john2789
ID: 9661655
ok, I will try now
0
 
LVL 1

Expert Comment

by:guitardude101
ID: 9661691
no assisist??
0
 

Author Comment

by:john2789
ID: 9661717
TO:guitardude101
how can I put the score on you, too?
0
 

Expert Comment

by:bojojo
ID: 9665651
hi

0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
port mapped I/o query 10 183
why debugging a macro i s difficult 10 33
Want to delete all my personal data 13 145
Digital Output does not have continuity to ground? 3 90
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now