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

help with String Library

37
Hi all,

I have been working on a quesstion, i have solved parts a up to c, but im having trouble with part d and e, any tips on how i can solve them would be very much appreaciated. I'd also appreaciate if you guys can also tell me if i have correctly solved a,b and c.

this is the question:

If string s1 and string s2 are two string variables, containing some strings how do you:
a.print out the number of characters of s1 to the screen

answer:

Expand|Select|Wrap|Line Numbers
  1.  string s1= “Easy”
  2.            cout<<s1.length()<<endl;
b.concatenate s1 and s2 and store the result in another string s

answer:

Expand|Select|Wrap|Line Numbers
  1. string s = s1+s2;
c.determine whether s2 contains the string "cool"

answer:
Expand|Select|Wrap|Line Numbers
  1. string s2("CSCI204 is cool");
  2.                           int where = s2.find("cool");
  3.                           cout << "First occurence of cool was found at: " << 
  4.                           where << endl;

d.display the string s1 in reverse order on the screen



e.display the lower-case version of the string s2 to the screen (Hint: you may want to create a separate function to return the lower-case version of a single character and use that function to solve this problem).

Thanks in advanance,

carly
Nov 27 '06 #1
1 1521
Ganon11
3,652 Expert 2GB
c.determine whether s2 contains the string "cool"

answer:
Expand|Select|Wrap|Line Numbers
  1. string s2("CSCI204 is cool");
  2. int where = s2.find("cool");
  3. cout << "First occurence of cool was found at: " << 
  4.         where << endl;
d.display the string s1 in reverse order on the screen

e.display the lower-case version of the string s2 to the screen (Hint: you may want to create a separate function to return the lower-case version of a single character and use that function to solve this problem).
First, for part c, what if the string did not have "cool" in it? What would .find() return? Depending on your compiler, it might return -1, or some garbage value - in my compiler (Bloodshed DevC++ 4.9.9.2), it returns a value string::npos. You should check to see if "cool" exists in the string before displaying where it is shown, or your code might display

First occurence of cool was found at: -1
// OR
First occurence of cool was found at: 47638238

which makes no sense whatsoever.

For parts d and e, consder the following fact:

A string is simply a collection of characters. (If you have discussed/covered arrays, you know that this collection is an array - a string is an array of characters.) You also know that the length of a string depends on the amount of characters - a space counts as much as a letter, etc. One of the cool things about a string is that you can get any single character if you know its position. A string is positioned in the following manner:

Expand|Select|Wrap|Line Numbers
  1.          // 0123456
  2. string s = "Sample."
  3. // The position of 'm' would be 2
If you wanted to print the 3rd character in s (which we know is 'm'), you can define a character variable ch and set ch to s[2], or the character at position 2 of s. In this way, you can access each individual character of any string.

Suppose you wanted to start processing the characters from the back. If the first position is 0, what is the position of the last character? It isn't s.length(), because in our previous example, s.length() is 7, but the characters go from 0 to 6. The last character is at position s.length() - 1.

Knowing this, you should be able to solve part d. (Note: If you have covered arrays, much of this should make sense. The positioning of each character of a string is the position of that character in an array. s.length() returns how many elements are in the array, and the last element of an array is its size - 1.)

For part e, all you should need to know is that the header file <cctype> includes the following function:

Expand|Select|Wrap|Line Numbers
  1. int tolower(int x);
  2. // tolower takes the integer representation of a character
  3. // (for example, 'A' is 65 and 'a' is 97) and returns the integer
  4. // representing the lowercase version IF the variable x
  5. // is an uppercase letter - otherwise, tolower returns
  6. // the same number.
  7. // Note that this function can be called by passing a character
  8. // instead of an integer, and will work perfectly well.
  9. // Also note that the return type is int - to convert to a character, use
  10. // char ch = static_cast<char>(/*Whatever*/);
Good luck!
Nov 27 '06 #2

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

Similar topics

4
by: Jim Hubbard | last post by:
I have some C# code that is supposed to wrap the defrag APIs and I am trying to convert it to VB.Net (2003). But, I keep having problems. The C# code is relatively short, so I'll post it...
4
by: Tad Johnson | last post by:
Hi all, I would not normally post about this issue but after a few hours of struggling maybe it's time for some help. I am a pascal programmer moving to C++. I am learning from a couple of...
6
by: Dave | last post by:
I'm struggling with something that should be fairly simple. I just don't know the regext syntax very well, unfortunately. I'd like to parse words out of what is basically a boolean search...
8
by: intrepid_dw | last post by:
Hello, all. I've created a C# dll that contains, among other things, two functions dealing with byte arrays. The first is a function that returns a byte array, and the other is intended to...
6
by: James Radke | last post by:
Hello, I have a multithreaded windows NT service application (vb.net 2003) that I am working on (my first one), which reads a message queue and creates multiple threads to perform the processing...
0
by: Jack Wu | last post by:
Hi I've spent a good majority of my day trying to figure out how to have PIL 1.1.5 working on my OSX 10.3.9_PPC machine. I'm still stuck and I have not gotten anywhere. Could somebody please...
8
by: Lucky | last post by:
hi guys! back again with another query. the problem is like this. i want to print a line like this: "---------------------------------------------" the easiest way is to simply assign it to...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
11
by: cybervigilante | last post by:
I can't seem to change the include path on my local winmachine no matter what I do. It comes up as includ_path .;C:\php5\pear in phpinfo() but there is no such file. I installed the WAMP package...
0
by: SOI_0152 | last post by:
Hi all! Happy New Year 2008. Il hope it will bring you love and happyness I'm new on this forum. I wrote a stored procedure on mainframe using DB2 7.1.1 and IBM language c. Everything works...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: 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
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...

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.