473,399 Members | 3,888 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.

String Character Conversion



Say I have a string read from a configuration file.

searchfor <tab> Needle\n\n

Where I want to search for the word "Needle" with two linefeeds. Now
when I read it from the file, it hasn't converted the linefeeds into
characters. Is there a function to do that or do I need to write it myself?

Jun 30 '06 #1
5 1694


Jayson Davis wrote On 06/30/06 11:46,:

Say I have a string read from a configuration file.

searchfor <tab> Needle\n\n

Where I want to search for the word "Needle" with two linefeeds. Now
when I read it from the file, it hasn't converted the linefeeds into
characters. Is there a function to do that or do I need to write it myself?


Please explain what you mean by "it hasn't converted ..."
The newline characters *are* characters, and can be read and
written just like vowels and consonants.

--
Er*********@sun.com

Jun 30 '06 #2
Eric Sosman wrote:

Jayson Davis wrote On 06/30/06 11:46,:
Say I have a string read from a configuration file.

searchfor <tab> Needle\n\n

Where I want to search for the word "Needle" with two linefeeds. Now
when I read it from the file, it hasn't converted the linefeeds into
characters. Is there a function to do that or do I need to write it myself?

Please explain what you mean by "it hasn't converted ..."
The newline characters *are* characters, and can be read and
written just like vowels and consonants.


Thanks for the reply. I'm taking a summer course in C and this is all a
tad overwhelming having it shoved into such a short timespan.

The string as read is "Needle\n\n", but what the string really needs to
be is "Needle\x0A0\x0A" in order for strstr() or strcmp() to work. In
other words, the string is literal "\n" and not '\n'.

I've gone through the string.h functions and did Google searches for
functions that would make replacement a little easier, but it would
appear I'm going to have to construct something myself. I'm curious if
there's a slick way to do it, or just trudge through one character at a
time.
Jun 30 '06 #3
On Fri, 30 Jun 2006 15:46:10 GMT, Jayson Davis
<ec************@yahoo.com> wrote in comp.lang.c:


Say I have a string read from a configuration file.

searchfor <tab> Needle\n\n

Where I want to search for the word "Needle" with two linefeeds. Now
when I read it from the file, it hasn't converted the linefeeds into
characters. Is there a function to do that or do I need to write it myself?


Your question is not clear. What do you mean by "it"? What "it"
hasn't converted the linefeeds into characters? Everything you can
ever read from any file is composed of characters.

Do you mean that, on a single line read from a text file, you
literally have the four characters '\', 'n', '\', 'n' as the last four
text characters before the end of the line?

If that's what you mean, you will need to interpret the meaning in
your program. The mapping of escape sequences like \n, \t, \b, and so
on, in string literals and character constants, to single characters
is a feature of the compiler and performed when translating the source
code at compile time. There is nothing in the standard library that
does this translation for you at run time.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jun 30 '06 #4


Jayson Davis wrote On 06/30/06 12:43,:
Eric Sosman wrote:
Jayson Davis wrote On 06/30/06 11:46,:

Say I have a string read from a configuration file.

searchfor <tab> Needle\n\n

Where I want to search for the word "Needle" with two linefeeds. Now
when I read it from the file, it hasn't converted the linefeeds into
characters. Is there a function to do that or do I need to write it myself?

Please explain what you mean by "it hasn't converted ..."
The newline characters *are* characters, and can be read and
written just like vowels and consonants.

Thanks for the reply. I'm taking a summer course in C and this is all a
tad overwhelming having it shoved into such a short timespan.

The string as read is "Needle\n\n", but what the string really needs to
be is "Needle\x0A0\x0A" in order for strstr() or strcmp() to work. In
other words, the string is literal "\n" and not '\n'.


Aha! I think I may have (*may* have) figured out
your difficulty. When you say the string as read is
"Needle\n\n", do you mean that there are ten characters
before the zero, the last four of which are "backslash,
n, backslash, n?" And that what you want is an eight-
character string ending in "newline, newline?"

If so, then no: There is nothing in the Standard
library that duplicates the operation of the C compiler
in translating escape sequences to characters. Those
escape sequences are a convention used only (as far as
C itself is concerned) in C source code, and the special
handling given to them by the compiler is not part of
the run-time library.

Your program, it seems, wants to use a similar
convention to represent "difficult" characters. If
that's what you're after, you'll need to write your
own translation code. Depending on how many of the C
source forms you decide to accept, the size of this
project could be anywhere from trivial to smallish.

--
Er*********@sun.com

Jun 30 '06 #5
Eric Sosman wrote:
Aha! I think I may have (*may* have) figured out
your difficulty. When you say the string as read is
"Needle\n\n", do you mean that there are ten characters
before the zero, the last four of which are "backslash,
n, backslash, n?" And that what you want is an eight-
character string ending in "newline, newline?"
That is exactly my difficulty. The string is ten characters and in
order to do the string comparison, I need it ending in "newline newline".
Your program, it seems, wants to use a similar
convention to represent "difficult" characters. If
that's what you're after, you'll need to write your
own translation code. Depending on how many of the C
source forms you decide to accept, the size of this
project could be anywhere from trivial to smallish.


That's what I was fearing. Oh well, maybe this lil' gotcha is why it is
part of the homework.

Thanks for your assistance and to the other folks who replied.
Jun 30 '06 #6

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

Similar topics

10
by: Achim Domma | last post by:
Hi, I read a webpage via urllib2. The result of the 'read' call is of type 'str'. This string can be written to disc via file('out.html','w').write(html). Then I write the string into a...
16
by: Christopher Benson-Manica | last post by:
I'm wondering about the best way to do the following: I have a string delimited by semicolons. The items delimited may be in any of the following formats: 1) 14 alphanum characters 2) 5...
6
by: KA Kueh | last post by:
Dear all, I have a requirement to replace the ' character with ASCII character (146) in C# but it seems that when I do the following the conversion is lost. char t = Convert.ToChar(146);...
5
by: Kasper Hansen | last post by:
I need to search through a binary file to find a specific string and then replace it with another string. However the System.Text.Encoding.ASCII.GetString method i originally used seems to do some...
6
by: karthi | last post by:
hi, I need user defined function that converts string to float in c. since the library function atof and strtod occupies large space in my processor memory I can't use it in my code. regards,...
30
by: Steve Edwards | last post by:
Hi, I'm re-writing some code that had relied on some platform/third-party dependent utility functions, as I want to make it more portable. Is there a standard C/C++/stl routine for changing an stl...
4
by: WaterWalk | last post by:
Hello, I'm currently learning string manipulation. I'm curious about what is the favored way for string manipulation in C, expecially when strings contain non-ASCII characters. For example, if...
13
by: Freaker85 | last post by:
Hello, I am new at programming in C and I am searching a manner to parse a string into an integer. I know how to do it in Java, but that doesn't work in C ;o) I searched the internet but I...
3
by: Kevin Frey | last post by:
I am porting Managed C++ code from VS2003 to VS2005. Therefore adopting the new C++/CLI syntax rather than /clr:oldSyntax. Much of our managed code is concerned with interfacing to native C++...
3
by: TamaThps | last post by:
I have an array of string values, and I need to access each character of the string individually. For instance I want my char variable to equal the first character in the string, and then compare it...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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.