strcpy
You do not need to program string manipulation as it is already part of the standard library. See std::string and std::wstring.
No.
C++ already provides a string class in the C++ standard template library. #include<iostream> #include<string> int main() { using namespace std; string s {"Hello world!"}; cout << s << endl; }
#include <iostream> #include <string> int main() { std::string myStr = ""; std::cout << std::endl << "Enter a string: "; std::cin >> myStr; system("PAUSE"); return 0; }
If you're using the std::string, why don't you just create another string instance like this: std::string hello = "hello"; std::string hello1; hello1 = hello; This will have copied the contents of the variable hello into hello1; char *psz1 = "This is a test."; char *psz2 = "..............."; // note - must be same or greater number of characters as psz1 char *p1 = psz1; char *p2 = psz2; while ((*p2++ = *p1++) != '\0'); // copy p1 to p2 Note: This is a trivial example, and might not work on most modern compilers that treat string literals as const or that place them into read-only memory. To solve this you need to replace the second line with ... char *psz2 = new char[16]; ... and then test to make sure that psz1 != NULL.
std::string::substr();
Use the following function to count the number of digits in a string. size_t count_digits (const std::string& str) { size_t count = 0; for (std::string::const_iterator it=str.begin(); it!=str.end(); ++it) { const char& c = *it; if (c>='0' && c<='9'); ++count; } return count; }
std::string input = ""; std::getline (std::cin, input); // get input from stdin std::stringstream ss (input); // place input in a string stream integer num = 0; if (ss >> num) // extract integer from string stream { // Success! } else { // Fail! }
#include<stdio.h> void copystr(char*,char*); main() { char*str1="I am a student of BCA 1"; char str2[30]; clrscr(); copystr(str2,str1); printf("\n %s",str2); } void copystr(char *dest,char *src) { while(*src!='\0') *dest++=*src++; *dest='\0'; return; }
#include<iostream> #include<string> int main() { std::string s("The quick brown fox jumps over the lazy dog"); std::cout<<s.c_str()<<std::endl; std::cout<<"The previous string is "<<s.size()<<" characters long."<<std::endl; }
Nothing.The C language only recognizes a few keywords, like "for" and "if". Most of what's in a C program ... that doesn't reference routines in the C program itself ... are library calls, and cputs() is one of those. What it does is write its argument (which should be a pointer to a character string) to the console... console put string.
You don't need a program to compare strings since std::string already provides support for all the comparison operators (<, <=, >, >=, == and !=). To roll your own you must first create a string class and then provide operator overloads for the comparison operators. To compare strings, start at the first character in each string and compare. So long as they remain equal, move onto the next character. The comparison ends as soon as any character differs. You need only compare these two characters to decide which string is the lesser. To perform a case insensitive comparison, copy the two characters and convert the copies to lower case (or upper case, it doesn't matter). Then compare the copies. Do this for each character as you compare them rather than converting the entire string.