Can anyone help me on strings??? I am a newbie and I have a problem dealing with string(school thing).

----------------------------------------------------

A program that checks if the substring "the" exist in the string inputted by the user.

* Use a function for checking.

-----------------------------------------------------

I'm in a big trouble right now... :(

C++ correct?

#include <string>
  using namespace std;
  
  bool StringExists(string check, string tocheck)
  {
  	if (tocheck.find(check, 0) == -1)
  		return false;
  	else
  		return true;
  }

ahhh... it is running perfectly on C++. But I have to code it on TURBO C. here, am I doing the right thing? :

#include<stdio.h>
#include<string.h>
main()
{
	char y;
	int x;
	clrscr();
	printf("Enter any word: ");
	scanf("%s",&y);
	x=strlen(y);
	printf("%d",x);
	getch();
}

This code does count string character but not correctly. hehe

sorry, wrong code. That code is to count the length of a string. But unfortunately, my code doesn't run perfectly. :(

Can anyone help me on strings??? I am a newbie and I have a problem dealing with string(school thing).

----------------------------------------------------

A program that checks if the substring "the" exist in the string inputted by the user.

* Use a function for checking.

-----------------------------------------------------

I'm in a big trouble right now... :(

char * strstr (const char *haystack, const char *needle)
It searches haystack for a substring needle rather than just a single character. It returns a pointer into the string haystack that is the first character of the substring, or a null pointer if no match was found. If needle is an empty string, the function returns haystack.

ok. I will try this one right away. Thanks for the logic. It really helps a lot.

peace. :) 8)

uhhmmm... hehehe I think I am trapped. I don't know how to code it.

uhhmmm... hehehe I think I am trapped. I don't know how to code it.

char *pt;

pt = strstr (input_string, "the");
if (pt != NULL)
printf ("Found \"the\" in the string");
else
printf ("Not found \"the\" in the string");

(just an example)

thanks man. But I also have this code and it is already running but I don't understand it and I don't know why I don't when I am the one who coded it. lol :D It just came up to my mind and I just typed it then the next thing, it was running... Here it is:

#include<stdio.h>

void again()
{
	char de;
	do
	{
		printf("\n\n\t\t\tDo it again? (y/n) ");
		de=getche();
		de=tolower(de);
	}
	while((de!='y')&&(de!='n'));
	if(de=='y')
		main();
	else
		exit(1);
}
int checkstr(char the[100])
{
	int i,value=0;
	if(strlen(the)>=99)
	{
		printf("String too long!");
		main();
	}
	else
	{
		for(i=0;i<strlen(the)+1;i++)
		{
			if((the[i]=='t')&&(the[i+1]=='h')&&(the[i+2]=='e'))
			{
				value=1;
				break;
			}
			else
				value=0;
		}
		return value;
	}
}
main()
{
	char the[100];
	int num;

	clrscr();
	printf("\n\n\n\tEnter any words EXCLUDING SPACES: ");
	scanf("%s",&the);
	num=checkstr(the);

	if(num==1)
		printf("\n\n\tThe substring `the' exist!");
	else
		printf("\n\n\tThe substring `the' does not exist!");

	again();
}

I put "EXCLUDING SPACES" cause the checkstring will check the first word then the other if the "the" string exist. hehehe :D

This article has been dead for over six months. Start a new discussion instead.