Thread: cstrings and strings

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    9

    cstrings and strings

    hello all I have written a program for a homework assignment, My code compiles without errors but I am absolutely stumped on a C string to string conversion:

    I need to handle the string as a cstring and as a string, so I will have two outputs one for the cstring and one for the string.

    In my code I opted to start this way: here's my snippet:

    Code:
    int main()
    {
    	char cString[200];
    	string stdString;  //here I was declaring  stdString for my conversion but I can't                       get the syntax right
    	
    //Get Input
    	cout << "Please enter a string: ";
    	cin.getline(cString, 200);
    	cout << "You entered: " << cString << endl;
    	cout << endl;
    any suggestions on resources or references that can help me solve my problem would be great.

  2. #2
    Registered User IdioticCreation's Avatar
    Join Date
    Nov 2006
    Location
    Lurking about
    Posts
    229
    You can use a normal string then when you want a cstring use s.c_str();

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Use std::getline() and remove cString all together.

    http://www.cplusplus.com/reference/string/getline.html

    gg

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    If you need to use a C style string and the C++ string class in the same program for learning purposes, then your code is correct and there is nothing wrong with it. It is missing headers and a namespace using directive, though. It also has no conversion taking place.

    If you are getting compiler errors, post your whole code including the #includes, then post the compiler errors.

    If you aren't getting errors but need help with the conversion, then simply assigning a C style string to the C++ string will make a "conversion" in that direction, and IdioticCreation's suggestion of .c_str() can be used to help convert from a C++ string to a C-style string.

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    9
    Hello Daved thanks for the reply, my code compiles fine. You said that there is no conversion taking place, thats what I'm trying to figure out how to do. I wanted to start with a C style string because at one point in the code I have a for loop check each character entered. I could have used a switch if I had gone the route of first inputing a string and not a C style string; however in the following code snippet how would I output "inputString" as a C style so that I can also use C style functions/manipulators on it?

    Code:
    #include <iostream>
    #include <string>
    #include <cstring>
    
    using namespace std;
    
    int main()
    {
    	string inputString;
    
    	cout << "Please input a string: " << inputString << endl;
    	getline(cin, inputString);
    	cout << "Your input string is: " << inputString;
    	cout << endl;
    	
    	return 0;
    }
    Thanks for your help.

  6. #6
    Registered User
    Join Date
    Feb 2008
    Posts
    9
    Would this be correct? :
    Code:
    #include <iostream>
    #include <string>
    #include <cstring>
    
    using namespace std;
    
    int main()
    {
    	string inputString;
    
    	cout << "Please input a string: " << inputString << endl;
    	getline(cin, inputString);
    	cout << "Your input string is: " << inputString << endl;
    	cout << "Your input string in C Style is: " << inputString.c_str();
    	cout << endl;
    	
    	return 0;
    }

  7. #7
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Correct. The c_str() method with give you a constant C-string, which you can use C style functions on as long as they do not modify the returned C-string.

    gg

  8. #8
    The larch
    Join Date
    May 2006
    Posts
    3,573
    however in the following code snippet how would I output "inputString" as a C style so that I can also use C style functions/manipulators on it?
    Chances are that std::string provides these functions and lets you do those manipulations much more easily and safely.

    Which functions and manipulations do you have in mind?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  9. #9
    Registered User
    Join Date
    Feb 2008
    Posts
    9
    I am trying to determine how many consonants and vowels are in the string. I have it working with this declaration:

    char cString[200];

    and with this for loop:
    Code:
    for(int x = 0; cString[x]!= '\0'; x++)
    	{
    		if(cString[x] == 'A'||cString[x] == 'E'||cString[x] == 'I'||cString[x] == 'O'||cString[x] == 'U'||cString[x] == 'Y',
    			cString[x] == 'a'||cString[x] == 'e'||cString[x] == 'i'||cString[x] == 'o'||cString[x] == 'u'||cString[x] == 'y')
    			vowels++;
    		else if (cString[x])
    			consonants++;
    	}
    this is why I originally wanted to covert from c style to c string, because I already have it looking and returning the # of consonants and vowels correctly.

  10. #10
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    same code will work if the cString is std::string, maybe just will small adjustment of the for condition
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  11. #11
    Registered User
    Join Date
    Feb 2008
    Posts
    9
    Hello Vart, what adjustments would you suggest.

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You can use iterators, so your for loop will look like
    Code:
    for (std::string::iterator it = mystring.begin(); it != mystring.end(); it++)
    and the character you're working with is then *it.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Personally, I feel that the comparison of characters will be much easier to read if you abstracted it out to an isVowel() function.

    You can use iterators, so your for loop will look like
    Yes, though you can still use string indices.

    Incidentally, it would be better to prefer ++it to it++ since the former is no slower than the latter and might be faster, with equal readability. One might also place the return value of mystring.end() in a variable so as to avoid calling the function on each iteration, but then it is a constant time operation so it is not too bad either way.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You might use this for loop just to maintain the similarities between the C-style and C++ string:
    Code:
    for(int x = 0; x < stdString.size(); ++x)
    x would still be an index and the rest of the code should be the same.

    Once you start getting used to the C++ string class you could then start using iterators as in tabstop's example, or perhaps change x to be declared as std::string;:size_type.

  15. #15
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Quote Originally Posted by dragongunner View Post
    I am trying to determine how many consonants and vowels are in the string. I have it working with this declaration:

    char cString[200];

    and with this for loop:
    Code:
    for(int x = 0; cString[x]!= '\0'; x++)
    	{
    		if(cString[x] == 'A'||cString[x] == 'E'||cString[x] == 'I'||cString[x] == 'O'||cString[x] == 'U'||cString[x] == 'Y',
    			cString[x] == 'a'||cString[x] == 'e'||cString[x] == 'i'||cString[x] == 'o'||cString[x] == 'u'||cString[x] == 'y')
    			vowels++;
    		else if (cString[x])
    			consonants++;
    	}
    this is why I originally wanted to covert from c style to c string, because I already have it looking and returning the # of consonants and vowels correctly.
    You can iterate a std::string with indices too:
    Code:
    for (size_t i = 0; i != string.size(); ++i)
    As to correctness, are you perhaps counting whitespace characters as consonants? Not sure, but is 'y' always counted as a vowel (as in "yellow" as opposed to "why")?

    To do less comparison, you could also use toupper or tolower, so you would have to check 'a' and 'A' etc separately.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Manipulating C strings
    By black_spot1984 in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2008, 10:23 AM
  2. Concatenating cstrings
    By trev456 in forum C++ Programming
    Replies: 3
    Last Post: 04-24-2007, 12:26 AM