473,386 Members | 1,720 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,386 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 30823
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
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...

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.