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

Dani AI

Generated

Brief expert note: the answers from (C++ string::find) and (strstr) are on the right track — those are the simplest, standard ways to look for a substring. The remaining problems in the thread come from input handling and a few unsafe/inefficient patterns in the posted C code: using a single char where a buffer is needed, using scanf("%s") which stops at spaces, calling strlen repeatedly inside a loop, and recursively calling main() to repeat the program.

Practical fixes and cautions: always allocate a buffer (for example char buf[256]) and prefer fgets() over scanf("%s") so the user can type spaces. Never use gets() and always limit reads to the buffer size to avoid overflow. Compute strlen() once if needed, or iterate with a pointer instead of recomputing length each loop. Avoid calling main() to repeat; use a loop. Note that clrscr()/getch() come from old Turbo C headers (conio.h) and are non‑standard — if you move to a modern compiler you can drop them.

Example (portable C — case insensitive, accepts spaces):

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

int contains_the(const char *s) {
    for (; *s; ++s) {
        if (tolower((unsigned char)*s) == 't' &&
            tolower((unsigned char)s[1]) == 'h' &&
            tolower((unsigned char)s[2]) == 'e')
            return 1;
    }
    return 0;
}

int main(void) {
    char buf[256];
    if (!fgets(buf, sizeof buf, stdin)) return 0;
    buf[strcspn(buf, "\n")] = 0; /* remove newline */
    puts(contains_the(buf) ? "Found \"the\"" : "Not found \"the\"");
    return 0;
}

If the requirement is to match "the" as a whole word (not inside "there" or "other"), use a word-boundary check — in Python that’s re.search(r'\bthe\b', s, re.I), and in C you can test surrounding characters for non-alpha or string boundaries.

Recommended Answers

All 8 Replies

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.