473,399 Members | 2,159 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

How do we check whether a string starts with a substring?

Dear all,

In C, how do we check whether a string starts with a substring? E.g.,
char *str = ...;
char *substr = ...;

How do we check whether str starts with substr?

Thank you very much!
Jul 12 '07 #1
3 30825
scruggsy
147 100+
Dear all,

In C, how do we check whether a string starts with a substring? E.g.,
char *str = ...;
char *substr = ...;

How do we check whether str starts with substr?

Thank you very much!
Forget about doing it in C for the moment.
How would you do it, if you were looking at the two strings written side by side on a piece of paper?
Write down the steps you would take, in English, then translate it into C.
If you need help with the code, post what you have so far.
Jul 12 '07 #2
Meetee
931 Expert Mod 512MB
Dear all,

In C, how do we check whether a string starts with a substring? E.g.,
char *str = ...;
char *substr = ...;

How do we check whether str starts with substr?

Thank you very much!

Hi,

If str = "Good Morning" and substr = "Good", you can compare substr in str until white space, and if both are same purpose is solved!

Regards
Jul 12 '07 #3
If str is your orginal string, substr is the substring you want to check, then
strncmp(str, substr, strlen(substr)) will return 0 if str starts with substr. The functions strncmp and strlen are in the C header file <string.h>
Dec 8 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Paul E Collins | last post by:
Given a string variable (form input), how can I determine whether it represents a valid integer? is_numeric is true for floats as well as integers, and is_int always fails on a string. P.
3
by: Ivan Demkovitch | last post by:
Hi! I would like to check if string contain specific substring. How to do it? P.S. I've done something stupid like String.Replace
6
by: Jan | last post by:
Hi, Is there any elegant way in VB.NET to check whether a certain string is part of a "collection" of strings. Something like IF "teststring" in {"string1", "string2", "string3",...
10
by: kd | last post by:
Hi All, Is there a command to check whether a given string is present in a text file, without having to read the contents of the file, a line at a time and then check for the given string? ...
5
by: Paul Morrison | last post by:
Hi I have looked at the PHP string functions and I cant see any function to check to see whether a string starts with a certain letter or letters. Is there any way of doing this with the built...
4
by: Jason | last post by:
I have a string that looks like this? ihelloworld_zzz_yyy In a single vb.net line, I want to remove the frist character and anything after and including the first "_" resulting in : ...
8
by: varsha.gadekar | last post by:
what is the easy way to check if the given string is substring of other string only using c++?
19
by: SPABBOJU | last post by:
Hi guys... I want to check whether string does not exist with out using !~ . basical for character we check like that using . How to do the same for word, sentence ?.... I...
3
Maidenz08
by: Maidenz08 | last post by:
How do i check whether an email id exists or not? I am following a three step validation process.. 1) syntax validation- which is pretty straight forward 2) DNS validation - I'm able to do...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.