473,387 Members | 1,542 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,387 software developers and data experts.

Searching string for characters

Hi, I've been experimenting some more with strings, I've written some
code where I want to test to see if a certain character char_a is in
the testlist string. Simple enough.
The problem is when testlist contains characters other than letters
and numbers such as comma, full stop and double quotes.

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

int main ()
{
char *testlist = "This is a test,."";
char char_a = 'a';

if (strchr(testlist,char_a) != NULL)
printf("Found in testlist \n");
else
printf("Not found in testlist \n");

return 0;
}

Upon compiling with gcc -Wall -Werror -W -O, I get
searchstring.c: In function `main':
searchstring.c:6: error: missing terminating " character
searchstring.c:7: error: parse error before "char"
searchstring.c:9: error: `char_a' undeclared (first use in this
function)
searchstring.c:9: error: (Each undeclared identifier is reported only
once
searchstring.c:9: error: for each function it appears in.)

The comma, full stop are no problems but it's the extra " that's
causing the issue.
The reason that I have a double quote in my testlist is that I want to
call another function if it's found.
Is it possible to get the extra double quote in my testlist or have I
gone about this the wrong way ?
Thank you for any advice.

Pat

May 17 '07 #1
2 1492
pt*****@gmail.com wrote:
The comma, full stop are no problems but it's the extra " that's
causing the issue.
The reason that I have a double quote in my testlist is that I want to
call another function if it's found.
To put a " in a string literal, you must escape it with a preceeding \.

Your C book should tell you that. If it doesn't, I'd call it broken.

--
"Our future looks secure, but it's all out of our hands." /Man and Machine/
- Magenta

Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England

May 17 '07 #2
pt*****@gmail.com said:
Hi, I've been experimenting some more with strings, I've written some
code where I want to test to see if a certain character char_a is in
the testlist string. Simple enough.
The problem is when testlist contains characters other than letters
and numbers such as comma, full stop and double quotes.

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

int main ()
{
char *testlist = "This is a test,."";
Make that:

char *testlist = "This is a test,.\"";

Better still:

const char *testlist = "This is a test,.\"";

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
May 17 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: jblazi | last post by:
I should like to search certain characters in a string and when they are found, I want to replace other characters in other strings that are at the same position (for a very simply mastermind game)...
4
by: tgiles | last post by:
Hi, all. Another bewildered newbie struggling with Python goodness. This time it's searching strings. The goal is to search a string for a value. The string is a variable I assigned the name...
8
by: Gordon Knote | last post by:
Hi can anyone tell me what's the best way to search in binary content? Best if someone could post or link me to some source code (in C/C++). The search should be as fast as possible and it would...
35
by: Cor | last post by:
Hallo, I have promised Jay B yesterday to do some tests. The subject was a string evaluation that Jon had send in. Jay B was in doubt what was better because there was a discussion in the C#...
12
by: Alexnb | last post by:
This is similar to my last post, but a little different. Here is what I would like to do. Lets say I have a text file. The contents look like this, only there is A LOT of the same thing. () A...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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
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.