Showing posts with label String. Show all posts
Showing posts with label String. Show all posts

Wednesday, September 1, 2010

Difference in string and pointers

Consider the assignments below:

char my_string[40] = "hello";
char my_name[] = "hello";
char *my_name = "hello";
Is there a difference between these? The answer is.. yes but after all they hold string called hello.
So where they are different?

In case1, the statement would allocate space for a 40 byte array and put the string in the first 6 bytes (five for the characters in the quotes and a 6th to handle the terminating '\0').

Consider the case 2: where we have only 6 characters assigned where the compiler would count the characters, leave room for the nul character and store the total of the 6 characters in memory the location of which would be returned by the array name, in this case my_name.

In case3 , the compiler store the strings of 6 bytes in the memory we don't know. So this is the array notation 6 bytes of storage in the static memory block are taken up, one for each character and one for the terminating nul character. But, in the pointer notation the same 6 bytes required, plus N bytes to store the pointer variable my_name (where N depends on the system but is usually a minimum of 2 bytes and can be 4 or more). 
For more read here.

These cases have their own significance.
Eg.
char p[] = "hello"; //This methods save space compared to char [40]= "hello";
char* p = "hello";//This methods save space compared to char [40]= "hello";

But consider the case of strcpy function....where we may have to copy one string larger than these 6 bytes of hello ... so what to do? To get to see the idea of problem see here.
So in case of
char p[] = "hello";
...
strcpy(p,"hello world"); 
We may get core dump in this case. Similar will the case hold in case of strcat, if we have these kind of strings.

In case of char *p = "hello";
we mean that string is hello somewhere in the memory and p points to it...but that string is
const char* and when we assign higher length value by strcpy or try to change it even will flash error message.
even this is wrong
strcpy("hello","world");
Because both "hello" and "world" are const strings, string literals.

How to store an array of C-strings

A typical problem is how to store an array of C-strings. C-strings are arrays of chars terminated by a zero value. How do we create an array of C-strings?

Possible Solution: Array of C-string Pointers

One common solution is to declare an array of pointers to C-strings, and dynamically allocate space for each of the C-strings.
char  aWord[100];   // Temporary place to hold input string.
char* words[1000]; // Array of pointers to c-strings
int n = 0; // Number of words
. . .
while (cin >> aWord) {
int len = strlen(aWord) + 1; // How much space is needed
char* newSpace = new char[len]; // Allocate with new
strcpy(newSpace, aWord); // Copy to new space
words[n] = newSpace; // Save pointer in array.
n++;
}
. . .

Another Solution: Two-dimensional array

Because C-strings are arrays, an array of C-strings could be implemented as a two-dimensional array. This approach may be attractive if the strings are all close in size, because the row length must accomodate the longest string.

Pointer to an array

When we say
string *p = "hello";

we actually doing are allocating hello\0 somewhere in memory we don't know and p points to that memory location.
"hello\0" is a string literal i.e. it is string constant so can't be edited. But in older c days, it was possible that you say
char *p =  "hello";
.....
p = "world";
and get away with it....but now it is not possible as concept of "const" has evolved.

But compilers still allow it for compatibility reasons, so that is still possible. To get stricter compiler, you can add -Wall -pedantic option to g++ and in practice one should do that.

Thursday, August 5, 2010

C Strings

Strings are arrays of chars. String literals are words surrounded by double quotation marks.
"This is a static string"
To declare a string of 49 letters, you would want to say:
char string[50];
This would declare a string with a length of 50 characters. Do not forget that arrays begin at zero, not 1 for the index number. In addition, a string ends with a null character, literally a '\0' character. However, just remember that there will be an extra character on the end on a string. It is like a period at the end of a sentence, it is not counted as a letter, but it still takes up a space. Technically, in a fifty char array you could only hold 49 letters and one null character at the end to terminate the string.

TAKE NOTE: char *arry; Can also be used as a string. If you have read the tutorial on pointers, you can do something such as:
arry = new char[256];
which allows you to access arry just as if it were an array. Keep in mind that to use delete you must put [] between delete and arry to tell it to free all 256 bytes of memory allocated.

For example:
delete [] arry.
Strings are useful for holding all types of long input. If you want the user to input his or her name, you must use a string. Using cin>> to input a string works, but it will terminate the string after it reads the first space. The best way to handle this situation is to use the function cin.getline. Technically cin is a class (a beast similar to a structure), and you are calling one of its member functions. The most important thing is to understand how to use the function however.

The prototype for that function is:
istream& getline(char *buffer, int length, char terminal_char);
The char *buffer is a pointer to the first element of the character array, so that it can actually be used to access the array. The int length is simply how long the string to be input can be at its maximum (how big the array is). The char terminal_char means that the string will terminate if the user inputs whatever that character is. Keep in mind that it will discard whatever the terminal character is.

It is possible to make a function call of cin.getline(arry, 50); without the terminal character. Note that '\n' is the way of actually telling the compiler you mean a new line, i.e. someone hitting the enter key.

For a example:
#include 

using namespace std;

int main()
{
char string[256]; // A nice long string

cout<<"Please enter a long string: ";
cin.getline ( string, 256, '\n' ); // Input goes into string
cout<<"Your long string was: "<< string <
cin.get();
}
Remember that you are actually passing the address of the array when you pass string because arrays do not require an address operator (&) to be used to pass their address. Other than that, you could make '\n' any character you want (make sure to enclose it with single quotes to inform the compiler of its character status) to have the getline terminate on that character.

cstring is a header file that contains many functions for manipulating strings. One of these is the string comparison function.
int strcmp ( const char *s1, const char *s2 );
strcmp will accept two strings. It will return an integer. This integer will either be:
Negative if s1 is less than s2.
Zero if s1 and s2 are equal.
Positive if s1 is greater than s2.
Strcmp is case sensitive. Strcmp also passes the address of the character array to the function to allow it to be accessed.
char *strcat ( char *dest, const char *src );
strcat is short for string concatenate, which means to add to the end, or append. It adds the second string to the first string. It returns a pointer to the concatenated string. Beware this function, it assumes that dest is large enough to hold the entire contents of src as well as its own contents.
char *strcpy ( char *dest, const char *src );
strcpy is short for string copy, which means it copies the entire contents of src into dest. The contents of dest after strcpy will be exactly the same as src such that strcmp ( dest, src ) will return 0.
size_t strlen ( const char *s );
strlen will return the length of a string, minus the termating character ('\0'). The size_t is nothing to worry about. Just treat it as an integer that cannot be negative, which it is.

Here is a small program using many of the previously described functions:
#include  //For cout
#include //For the string functions

using namespace std;

int main()
{
char name[50];
char lastname[50];
char fullname[100]; // Big enough to hold both name and lastname

cout<<"Please enter your name: ";
cin.getline ( name, 50 );
if ( strcmp ( name, "Julienne" ) == 0 ) // Equal strings
cout<<"That's my name too.\n";
else // Not equal
cout<<"That's not my name.\n";
// Find the length of your name
cout<<"Your name is "<< strlen ( name ) <<" letters long\n";
cout<<"Enter your last name: ";
cin.getline ( lastname, 50 );
fullname[0] = '\0'; // strcat searches for '\0' to cat after
strcat ( fullname, name ); // Copy name into full name
strcat ( fullname, " " ); // We want to separate the names by a space
strcat ( fullname, lastname ); // Copy lastname onto the end of fullname
cout<<"Your full name is "<< fullname <<"\n";
cin.get();
}

Safe Programming

The above string functions all rely on the existence of a null terminator at the end of a string. This isn't always a safe bet. Moreover, some of them, noticeably strcat, rely on the fact that the destination string can hold the entire string being appended onto the end. Although it might seem like you'll never make that sort of mistake, historically, problems based on accidentally writing off the end of an array in a function like strcat, have been a major problem.

Fortunately, in their infinite wisdom, the designers of C have included functions designed to help you avoid these issues. Similar to the way that fgets takes the maximum number of characters that fit into the buffer, there are string functions that take an additional argument to indicate the length of the destination buffer. For instance, the strcpy function has an analogous strncpy function
char *strncpy ( char *dest, const char *src, size_t len );
which will only copy len bytes from src to dest (len should be less than the size of dest or the write could still go beyond the bounds of the array). Unfortunately, strncpy can lead to one niggling issue: it doesn't guarantee that dest will have a null terminator attached to it (this might happen if the string src is longer than dest). You can avoid this problem by using strlen to get the length of src and make sure it will fit in dest. Of course, if you were going to do that, then you probably don't need strncpy in the first place, right? Wrong. Now it forces you to pay attention to this issue, which is a big part of the battle.

Friday, April 9, 2010

Given a string s1 and a string s2, write a snippet to say whether s2 is a rotation of s1 using only one call to strstr routine? (eg given s1 = ABCD and s2 = CDAB, return true, given s1 = ABCD, and s2 = ACBD , return false)

Syntax of strstr 
const char * strstr ( const char * str1, const char * str2 );

 Returns a pointer to the first occurrence of str2 in str1, or a null pointer if str2 is not part of str1.
The matching process does not include the terminating null-characters.

Approach1
Append str1 to itself and do a strstr for str2. That should work.
(I am assuming strstr looks for string x within string y).
But this fails sometime...for eg. s1 = ABCDA, s2 = BCDA, s2s2 = BCDABCDA
s2s2 now contains s1, but it is not a rotation of s1.
probably other corner cases exist. 

So we can improve it...by first checking whether s1 and s2 have equal number of characters or not.

Sunday, December 6, 2009

String conversion functions

Three functions take a string as an argument and convert it to a number of the type shown below:
#include <stdlib.h>
double atof(const char *nptr);
long atol(const char *nptr);
int atoi(const char *nptr);
For each of the functions, the number is converted and the result returned. None of them guarantees to set errno (although they may do in some implementations), and the results of a conversion which overflows or cannot be represented is undefined.
Most of these are fairly straightforward to use. For example:
char *str1 = "100";
char *str2 = "55.444";
char *str3 = " 1234";
char *str4 = "123four";
char *str5 = "invalid123";

int i;
float f;

i = atoi(str1); /* i = 100 */
f = atof(str2); /* f = 55.44 */
i = atoi(str3); /* i = 1234 */
i = atoi(str4); /* i = 123 */
i = atoi(str5); /* i = 0 */
Note:
  • Leading blank characters are skipped.
  • Trailing illegal characters are ignored.
  • If conversion cannot be made zero is returned and errno  is set with the value ERANGE.  
More sophisticated functions are:
#include <stdlib.h>
double strtod(const char *nptr, char **endptr);
long strtol(const char *nptr, char **endptr, int base);
unsigned long strtoul(const char *nptr,char **endptr, int base);
All three functions work in a similar way. Leading white space is skipped, then a subject sequence, resembling an appropriate constant, is found, followed by a sequence of unrecognized characters. The trailing null at the end of a string is always unrecognized. The subject sequence can be empty. The subject sequences are determined as follows:
strtod
Optional + or -, followed by a digit sequence containing an optional decimal point character, followed by an optional exponent. No floating suffix will be recognized. If there is no decimal point present, it is assumed to follow the digit sequence.
strtol
Optional + or -, followed by a digit sequence. The digits are taken from the decimal digits or an upper or lower case letter in the range a–z of the English alphabet; the letters are given the values 10–35 respectively. The base argument determines which values are permitted, and may be zero, or otherwise 2–36. Only ‘digits’ with a value less than that of base are recognized. A base of 16 permits the characters 0x or 0X to follow the optional sign. A base of zero permits the input of characters in the form of a C integer constant. No integer suffix will be recognized.
strtoul
Identical to strtol but with no sign permitted.
If endptr is non-null, the address of the first unrecognized character is stored in the object that it points to. If the subject sequence is empty or has the wrong form, this is the value of nptr.
If a conversion can be performed, the functions convert the number and return its value, taking into account a leading sign where permitted. Otherwise they return zero. On overflow or error the action is as follows:
strtod
On overflow, returns ±HUGE_VAL according to the sign of the result; on underflow, returns zero. In either case, errno is set to ERANGE.
strtol
On overflow, LONG_MAX or LONG_MIN is returned according to the sign of the result, errno is set to ERANGE.
strtoul
On overflow, ULONG_MAX is returned, errno is set to ERANGE.
If the locale is not the "C" locale, there may be other subject sequences recognised depending on the implementation.

Saturday, December 5, 2009

C code to return a string from a function

This C program wont work!


char *myfunction(int n)
{
   char buffer[20];
   sprintf(buffer, "%d", n);
   return retbuf;
}



This wont work either!


char *myfunc1()
{
  char temp[] = "string";
  return temp;
}


char *myfunc2()
{
   char temp[] = {'s', 't', 'r', 'i', 'n', 'g', '\0'};
   return temp;
}


This will work
The returned pointer should be to a static buffer (like static char buffer[20];), or to a buffer passed in by the caller function, or to memory obtained using malloc(), but not to a local array. 


char *myfunc()
{
   char *temp = "string";
   return temp;
}

int main()
{
   puts(myfunc());
}



So will this


calling_function()
{
  char *string;
  return_string(&string);
  printf(?\n[%s]\n?, string);
}

boolean return_string(char **mode_string /*Pointer to a pointer! */)
{
   *string = (char *) malloc(100 * sizeof(char));
   DISCARD strcpy((char *)*string, (char *)?Something?);
}

To check whether string is pallindrome

Eg, of pallindrome = Malayalam
void isPalindrome(char *string)
{
  char *start, *end;

  if(string)
  {
     start = string;
     end   = string + strlen(string) - 1;

     while((*start == *end) && (start!=end))
     {
       if(start
       if(end>start)end--;
     }

     if(*start!=*end)
     {
        printf("\n[%s] - This is not a palidrome!\n", string);
     }
     else
     {
        printf("\n[%s] - This is a palidrome!\n", string);
     }
  }
  printf("\n\n");
}

Given two strings A and B, how would you find out if the characters in B were a subset of the characters in A?

#include
#include

int isSubset(char *a, char *b);

int main()
{
 char str1[]="defabc";
 char str2[]="abcfed";

 if(isSubset(str1, str2)==0)
 {
   printf("\nYes, characters in B=[%s] are a subset of characters in A=[%s]\n",str2,str1);
 }
 else
 {
   printf("\nNo, characters in B=[%s] are not a subset of characters in A=[%s]\n",str2,str1);
 }

 getch();
 return(0);
}


// Function to check if characters in "b" are a subset
// of the characters in "a"

int isSubset(char *a, char *b)
{
 int letterPresent[256];
 int i;

 for(i=0; i<256; i++)
    letterPresent[i]=0;

 for(i=0; a[i]!='\0'; i++)
    letterPresent[a[i]]++;

 for(i=0; b[i]!='\0'; i++)
    if(!letterPresent[b[i]])
       return(1);

 return(0);
}

In this case isSubset returns true.

Sunday, August 17, 2008

String Class in cpp(Standard cpp strings)

Here is how to test the string class already present in cpp.
To use simple strings u have to include <iostream>. For example consider the program,
#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    string word="hello";
    cout<<word;
    getch();
   
}

There are various functions and operators in this string class. These operators are present in string -
=        assignment
+ , +=  concatenation etc
 == , != . < , <= , >= , >   equality etc.
[]       subscription
<<     output
>>     input

Following are the functions which are present in the string class

Consider the program:
#include <iostream>
#include <conio.h>
using namespace std;
int main( )
{
    string s1 ( "kicit" ) ;
    string s2 ( "Nagpur" ) ;

    cout << endl ;
    cout << "String s1: " ;
    cout<<s1;
    cout << "\nLength of string s1: " << s1.length( ) << endl ;

    cout << "\nString s2: " ;
    cout<<s2;

    string s3 = s1;// copy
    cout<<"\ns3="<<s3;

    s3=s3+s1;
    cout << "\nString s3 after concatenation: " ;
    cout<<s3;

    if ( s1.compare(s2)== 0 )
        cout << "\nThe strings s1 and s2 are similar" << endl ;
    else
        cout << "\nThe strings s1 and s2 are not similar" << endl ;
        getch();
}



Some more cases of string:
string s1;                                    //s1 contains 0 character
string s2 =  "New York";            
//s1 contains 8 character
string s3(60,'*');                          //s1 contains 60 asterisk
string s4=s3;                             //s1 contains 60 asterisk
string s5(s2, 4,2);                      //s1 contains 2 characters , i.e. "Yo"
Some more:
string s= "ABCDEFG";
getline(s,cin);           // reads the entire string with spaces into s
char c = s[2];           // c = C;
s[4]='*';                   //s= "ABCD*FG";

const char * cs = s.c_str();   // CONVERT s TO C STRING

Using find, substr(), erase() and replace
s6=s+"HIJK";
s4= s6.substr();
s6.erase(4, 2);
s6.replace(5,2,  "xyz")
s6 = "Missisippi River";












hello i am not hter
constructors 1.


#include
#include
using namespace std;

int main ()
{
char *line = "short line for testing";

// with no arguments
string s1;
s1 = "Anatoliy";
cout << "s1 is: " << s1 << endl;

// copy constructor
string s2 (s1);
cout << "s2 is: " << s2 << endl;

// one argumen
string s3 (line);
cout << "s3 is: " << s3 << endl;

// first argumen C string
// second number of characters
string s4 (line,10);
cout << "s4 is: " << s4 << endl;

// 1 - C++ string
// 2 - start position
// 3 - number of characters
string s5 (s3,6,4); // copy word 'line' from s3
cout << "s5 is: " << s5 << endl;

// 1 - number characters
// 2 - character itself
string s6 (15,'*');
cout << "s6 is: " << s6 << endl;

// 1 - start iterator
// 2 - end iterator
string s7 (s3.begin(),s3.end()-5);
cout << "s7 is: " << s7 << endl;

// you can instantiate string with assignment
string s8 = "Anatoliy";
cout << "s8 is: " << s8 << endl;

return 0;
}
OUTPUT:
// s1 is: Anatoliy
// s2 is: Anatoliy
// s3 is: short line for testing
// s4 is: short line
// s5 is: line
// s6 is: ***************
// s7 is: short line for te
// s8 is: Anatoliy



getline 1.



/* 1 getline ( intut_stream, str, delim );
Extracts characters from intut_stream and stores them
in str until s.max_size() characters have been extracted,
the end of file occurs, or delim is encountered, in which
case delim is extracted from istr but is not stored in s
2 getline( Iter, str )
Inputs a string value for str as in the preceding func�
tion with delim =
*/
#include
#include
#include
#include
using namespace std;

int main ()
{
string str;
cout << "Enter string (EOL = $) : ";
getline (cin, str, '$');
cout << "Str is : " << str << endl;

ifstream In("data.dat");
vector v;

cout << endl << "Read data from file" << endl;
while ( ! In.eof() )
{
getline (In, str);
v.push_back(str);
}

copy (v.begin(),v.end(),
ostream_iterator(cout,"\n"));
cout << endl;


return 0;
}
OUTPUT:
// Enter string (EOL = $) : Str is : first line
// second line$
//
// Read data from file
// file: "data.dat"
// second line
// last line



<< >> operators 1.


#include
#include
using namespace std;

int main ()
{
string str;

cout << "Enter string for testing : ";
cin >> str;
cout << "\nString is : " << str << endl;

cout << "Enter string for testing "
<< "(d to quit) : ";
while ( cin >> str )
{
cout << endl;
cout << "String is : " << str << endl;
cout << "Enter string for testing "
<< "(d to quit) : ";
}

return 0;
}
OUTPUT:
// Enter string for testing : first
// String is : first
// Enter string for testing (d to quit) : second
// String is : second
// Enter string for testing (d to quit) : third
// String is : third
// Enter string for testing (d to quit) :



+ += = operators 1.


#include
#include
using namespace std;

int main ()
{
string str = "Hello";
cout << "str is : " << str << endl;
str += ",";
str += ' ';
cout << "str is : " << str << endl;

string s;
s = str + "World";
cout << "s is : " << s << endl;

char ch = '!';
s += ch;
cout << "s is : " << s << endl;

return 0;
}
OUTPUT:
// str is : Hello
// str is : Hello,
// s is : Hello, World
// s is : Hello, World!



append 1.


#include
#include
using namespace std;

int main ()
{
string str = "Nobody is perfect";
string s = ""; // empty string
char *ch = "abcdef";

// append string str at the end of s;
// return s

// appends at the end of s a copy of the n characters
// in str, starting at position pos; if n is too
// large, characters are copied only until the end
// of str is reached;
// returns s
s.append(str,0,6);
cout << "s is : " << s << endl;

// appends copies of the characters in the range [inpIt1,
// inpIt2] to s; returns s
string::iterator inpIt1 = str.begin()+6;
//start from ' is'
string::iterator inpIt2 = str.end();

s.append(inpIt1,inpIt2);
cout << "s is : " << s << endl;

// appends three !
s.append(3,'!');
cout << "s is : " << s << endl;

// appends the first n characters in ch at the end
// of s; returns s
s.append(ch,3);
cout << "s is : " << s << endl;

// appends charArray at the end of s; returns s
s.append(ch,3);
cout << "s is : " << s << endl;

return 0;
}
OUTPUT:
// s is : Nobody
// s is : Nobody is perfect
// s is : Nobody is perfect!!!
// s is : Nobody is perfect!!!abc
// s is : Nobody is perfect!!!abcabc



assign 1.


#include
#include
using namespace std;

int main ()
{
string str = "Nobody is perfect";
string s = "";
char *ch = "Robert Frost";

// assigns a copy of str to s; returns s
s.assign(str);
cout << "s is : " << s << endl;


// assigns to s a copy of the n characters in str, start�
// ing at position 10: if n is too large, characters are
// copied only until the end of str is reached: returns s

s.assign(str,10,7); // perfect
cout << "s is : " << s << endl;

// assigns to s a string consisting of the first n charac�
// ters in ch: returns s

s.assign(ch,6);
cout << "s is : " << s << endl;

// assigns to s a copy of ch: returns s
s.assign(ch);
cout << "s is : " << s << endl;

// assigns to s a string consisting of the characters in
// the range str.begin(), str.end(); returns s

s.assign(str.begin(),str.end());
cout << "s is : " << s << endl;

// assigns to s a string consisting of n copies of ch;
// returns s

s.assign(17,'*');
cout << "s is : " << s << endl;

return 0;
}
OUTPUT:
// s is : Nobody is perfect
// s is : perfect
// s is : Robert
// s is : Robert Frost
// s is : Nobody is perfect
// s is : *****************



at 1.


// returns s[pos]
#include
#include
using namespace std;

int main ()
{
string s = "Nobody is perfect";

// Returns s[pos]
for ( int pos = 0; pos < s.length(); ++pos )
cout << s.at(pos) << " ";
cout << endl;

return 0;
}
OUTPUT:
// N o b o d y i s p e r f e c t


begin 1.


// Returns an iterator positioned at the
// first character in a string
#include
#include
using namespace std;

int main ()
{
string str = "C++ is best computer language";

string::iterator It = str.begin();

while ( It != str.end() )
{
if ( *It == ' ' )
*It = '\n';
cout << *It++;
}
cout << endl;

return 0;
}
OUTPUT:
// C++
// is
// best
// computer
// language



c_str 1.


// returns (the base address of) a char
// array containing the characters stored in s,
// terminated by a null character.
#include
#include
using namespace std;

int main ()
{
string str = "Anatoliy";
char *ary = new char[str.length()+1];

// strcpy ( ary, str ); that is wrong way
strcpy ( ary, str.c_str() ); // that is correct

cout << ary << endl;

return 0;
}
OUTPUT:
// Anatoliy


capacity 1.


// returns the size (of type size_type)
// of the storage allocated in string
#include
#include
using namespace std;

int main ()
{
string str = "C++ is best computer language";

string::size_type cap;
cap = str.capacity();

cout << "Capacity of str is: " << cap << endl;
cout << "Size of str is : " << str.size()
<< endl;
cout << "Length of str is : " << str.length()
<< endl;

cout << "Resize the str for 50 character" << endl;
str.resize(50);
cap = str.capacity();

cout << "Capacity of str is: " << cap << endl;
cout << "Size of str is : " << str.size()
<< endl;
cout << "Length of str is : " << str.length()
<< endl;

return 0;
}
OUTPUT:
// Capacity of str is: 32
// Size of str is : 29
// Length of str is : 29
// Resize the str for 50 character
// Capacity of str is: 64
// Size of str is : 50
// Length of str is : 50



compare 1.


#include
#include
using namespace std;

int main ()
{
string str1 = "string";
string str2 = "String";
string str3 = "second string";
char ch[] = "first string";

cout << "string str1 is : " << str1 << endl;
cout << "string str2 is : " << str2 << endl;
cout << "char ary ch is : " << ch << endl;
cout << "string str3 is : " << str3 << endl;
cout << endl;

// compare str1 and str2
cout << "1." << endl;
size_t comp = str1.compare(str2);
cout << "String str1 is ";
( comp == 0 ) ? cout << "equal" : cout
<< "not equal";
cout << " to string str2" << endl;

// compare str1 and literal string "string"
cout << "2." << endl;
comp = str1.compare("string");
cout << "String str1 is ";
( comp == 0 ) ? cout << "equal" : cout
<< "not equal";
cout << " to array of char \"string\"" << endl;

// 3. and 4. doesn't work with Microsoft
// Visual Studio compiler


// compare str3 start from pos 7 to 5
// with str1

cout << "3." << endl;
comp = str3.compare(str1,7,5);
cout << "Part of string str3 is ";
( comp == 0 ) ? cout << "equal" : cout
<< "not equal";
cout << " to str1" << endl;

// compare str3 start from pos 7
// with literal string "string"

cout << "4." << endl;
comp = str3.compare("string",7);
cout << "Part of string str3 is ";
( comp == 0 ) ? cout << "equal" : cout
<< "not equal";
cout << " to C string \"string\"" << endl;

// next 4 'compare' functions
// doesn't work with GNU compiler


cout << "5." << endl;
comp = str1.compare(6,10,ch);
cout << "String str1 is ";
( comp == 0 ) ? cout << "equal" : cout
<< "not equal";
cout << " to part of char ary \"first string\""
<< endl;

cout << "6." << endl;
comp = str1.compare(0,3,str3);
cout << "Part of str1 is ";
( comp == 0 ) ? cout << "equal" : cout
<< "not equal";
cout << " to string \"second string\""
<< endl;

cout << "7." << endl;
comp = str1.compare(1,3,str2,1,3);
cout << "String str1 is ";
( comp == 0 ) ? cout << "equal" : cout
<< "not equal";
cout << " to part of string \"second string\""
<< endl;

cout << "8." << endl;
comp = str1.compare(1,3,str2,1,3);
cout << "String str1 is ";
( comp == 0 ) ? cout << "equal" : cout
<< "not equal";
cout << " to part of string \"second string\""
<< endl;

return 0;
}
OUTPUT: GNU compiler
// string str1 is : string
// string str2 is : String
// char ary ch is : first string
// string str3 is : second string
//
// 1.
// String str1 is not equal to string str2
// 2.
// String str1 is equal to array of char "string"
// 3.
// Part of string str3 is equal to str1
// 4.
// Part of string str3 is equal to C string "string"
// 5.
// 6.
// 7.
// 8.

OUTPUT: Microsoft Visual Studio compiler
// string str1 is : string
// string str2 is : String
// char ary ch is : first string
// string str3 is : second string
//
// 1.
// String str1 is not equal to string str2
// 2.
// String str1 is equal to array of char "string"
// 3.
// 4.
// 5.
// String str1 is not equal to part of char ary "first
// string"
// 6.
// Part of str1 is not equal to string "second string"
// 7.
// String str1 is equal to part of string "second string"
// 8.
// String str1 is equal to part of string "second string"
// Press any key to continue


copy 1.


#include
#include
using namespace std;

int main ()
{
string str = "First Name: Robert";
char fname[255];
cout << "str is: " << str << endl;

int n = str.find(':');

str.copy(fname, // copy to array
n+1, // how many char
0); // start position from str

// must terminate fname with '\0';
fname[n+1] = 0;

cout << "fname is: " << fname << endl;

return 0;
}
OUTPUT:
// str is: First Name: Robert
// fname is: First Name:



empty 1.


#include
#include
using namespace std;

int main ()
{
string str = "*******";

while ( ! str.empty() )
{
cout << str << endl;
str.erase(str.end()-1);
}
cout << endl;

return 0;
}
OUTPUT:
// *******
// ******
// *****
// ****
// ***
// **
// *



end 1.


// returns an iterator porsitioned immediately
// after the last character in string
#include
#include
using namespace std;

int main ()
{
string s;
string str = "*************************";
size_t pos = str.length();

while ( pos )
{
s.assign ( str.begin(),str.end() - pos+1);
cout << s << endl;
pos -= 5;
}

return 0;
}
OUTPUT:
// *
// ******
// ***********
// ****************
// *********************



erase 1.


#include
#include
#include
using namespace std;

int main ()
{
string str, s;

for ( char ch = 'a'; ch <= 'z'; ch++ )
str.append(1,ch);
s = str;
cout << "str is: " << str << endl;
cout << "s is: " << str << endl;

// removes 13 characters from the beginning
str.erase(0,13);
cout << "Erased range fron str : " << str << endl;
// removes 13 characters starts from 14
str = s.erase(13,13);
cout << "Erased range from s : " << str << endl;

// removes one character pointed by s.begin()
cout << endl << "Erase one, second character from s"
<< endl;
s.erase(s.begin()+1);
cout << "s is: " << s << endl;

// removes range of characters
s.erase(s.begin(),s.begin()+4);
cout << "s is: " << s << endl;

return 0;
}
OUTPUT:
// str is: abcdefghijklmnopqrstuvwxyz
// s is: abcdefghijklmnopqrstuvwxyz
// Erased range fron str : nopqrstuvwxyz
// Erased range from s : abcdefghijklm
//
// Erase one, second character from s
// s is: acdefghijklm
// s is: fghijklm



find 1.


#include
#include
#include
using namespace std;

int main ()
{
string str("C++ is best language");
int pos1, pos2; // size_t or size_type
// work not correct
// search for first string "best" inside of str
// default position is 0

pos1 = str.find ("best");
cout << "Word best is found on position " << pos1+1
<< endl;

// if pattern is not found - return -1
pos2 = str.find ("best",pos1+1);
cout << "Word best is found on position " << pos2+1
<< endl;

// search for first occurrence of character
pos1 = str.find('g');
cout << "First character 'g' found on position "
<< pos1
<< endl;

// search for first occurrence of string
string s = "is";
pos1 = str.find (s);
cout << "Word 'is' is found on position " << pos1+1
<< endl;

return 0;
}
OUTPUT:
// Word best is found on position 8
// Word best is found on position 0
// First character 'g' found on position 15
// Word 'is' is found on position 5



find_first_not_of 1.


#include
#include
using namespace std;

int main ()
{
string str("C++ is best language");
cout << "str is: " << str << endl;

int n = str.find_first_not_of("aeiouAEIOU");

cout << "First consonant found at " << n+1
<< " position" << endl;

return 0;
}
OUTPUT:
// str is: C++ is best language
// First consonant found at 1 position



find_first_not_of 2.


#include
#include
using namespace std;

int main ()
{
string str("C++ is best language");
cout << "str is: " << str << endl;

// search first not ' ',
// start from position 7

int n = str.find_first_not_of(' ',7);
cout << "first not of space character "
<< "found at position " << n+1 << endl;

return 0;
}
OUTPUT:
// str is: C++ is best language
// first not of space character found at position 8



find_first_not_of 3.


#include
#include
using namespace std;

int main ()
{
string str("C++ is best language");
string s = "C++";

cout << "str is: " << str << endl;

// search character from pattern
// using the first x ch�racters in pattern.
// the value position must be given

int n = str.find_first_not_of("CBCD",0,3);
cout << "first not 'C' is found at position "
<< n+1 << endl;

// search first not of
// pattern is string

n = str.find_first_not_of(s);
cout << "first not of C++ is found "
<< "at position " << n+1 << endl;

return 0;
}
OUTPUT:
// str is: C++ is best language
// first not 'C' is found at position 2
// first not of C++ is found at position 4



find_first_of 1.


#include
#include
using namespace std;

int main ()
{
string str("C++ is best language");
string s = "be";

cout << "str is: " << str << endl;

// search be start from position 2
// if position is ommited - default is 0

int n = str.find_first_of(s,2);
cout << "first 'be' found at position "
<< n+1 << endl;

// same as above but search for character
n = str.find_first_of('l');
cout << "first character 'l' found at "
<< "position " << n+1 << endl;

// search 'first of' for the characters in
// charary

char charary[] = " bea";
cout << "charary[] = \" bea\"" << endl;
n = str.find_first_of(charary,0);
cout << "first character from charary "
<< "found at position " << n+1 << endl;
cout << "Note: position 4 is space" << endl;

// same as above but third argumen is
// number of character from which searching
// starts
// this variant of find_first_of dosn't
// work properly with GNU compiler

n = str.find_first_of(" bae",0,3);
cout << "first character from charary "
<< "found at position " << n+1 << endl;

return 0;
}
OUTPUT:
// str is: C++ is best language
// first 'be' found at position 8
// first character 'l' found at position 13
// charary[] = " bea"
// first character from charary found at position 4
// Note: position 4 is space
// first character from charary found at position 4



find_last_not_of 1.


#include
#include
using namespace std;

int main ()
{
string str("C++ is best language");
string s = "langue";
int pos = str.length()-1;
cout << "str is: " << str << endl;

// returns the highest position <= pos of a character
// in str that does not match any charcter in s;
// returns nopos if there is no such position:
// npos is the default value for pos

int n = str.find_last_not_of(s, pos);
cout << "last_not_of 'langue' found at position "
<< n+1 << endl;

// same as above but search for single character
n = str.find_last_not_of('e');
cout << "last_not_of 'e' found at position "
<< n+1 << endl;

char ary[] = "be";
// seawrch for occurence last_not_of
// from pattern ary in str

n = str.find_last_not_of(ary);
cout << "last_not_of 'be' found at position "
<< n+1 << endl;

return 0;
}
OUTPUT:
// str is: C++ is best language
// last_not_of 'langue' found at position 12
// last_not_of 'e' found at position 19
// last_not_of 'be' found at position 19



find_last_of 1.


#include
#include
using namespace std;

int main ()
{
string str("C++ is best language");
string s = "g";
cout << "str is: " << str << endl;
cout << "s is: " << s << endl;

int n = str.find_last_of(s);
cout << "last_of '" << s << "' faund"
<< " at position " << n+1 << endl;

n = str.find_last_of(' ');
cout << "last_of ' ' faund"
<< " at position " << n+1 << endl;

n = str.find_last_of(" la");
cout << "last_of \" la\" faund"
<< " at position " << n+1 << endl;

return 0;
}
OUTPUT:
// str is: C++ is best language
// s is: g
// last_of 'g' faund at position 19
// last_of ' ' faund at position 12
// last_of " la" faund at position 18



insert 1.


#include
#include
using namespace std;

int main ()
{
string str = "C++ language";
string s = "is best";
char ch[] = "C++ language";

cout << "str is: " << str << endl;
cout << "s is: " << s << endl;
cout << "ch is: " << s << endl;

// insert a copy of s into str
// at position pos;

string::size_type pos = 4;
str.insert(pos,s);
cout << "str is: " << str << endl;

// insert a copy of ch into str at
// the position specified by iterator
// return an iterator positioned at
// this copy

int n = str.find('l');
str.insert(str.begin() + n,' ');
cout << "str is: " << str << endl;

// like above but n x copies of char
str.insert(str.end(),3,'!');
cout << "str is: " << str << endl;

// insert 4 char from ch into s
// at the position 0

s.insert(0,ch,4);
cout << "s is: " << s << endl;

// insert 8 characters from str
// start from position n ('langu...')
// into s at position x (end string)

n = str.find('l');
int x = s.length();
s.insert(x,str,n,8);
cout << "s is: " << s << endl;

n = s.find('l');
s.insert(s.begin()+n,' ');
cout << "s is: " << s << endl;

// insert range (begin - begin+7) of str
// into s at position begin+4

s.insert(s.begin()+4,str.begin(),str.begin()+7);
cout << "s is: " << s << endl;
return 0;
}
OUTPUT:
// str is: C++ language
// s is: is best
// ch is: is best
// str is: C++ is bestlanguage
// str is: C++ is best language
// str is: C++ is best language!!!
// s is: C++ is best
// s is: C++ is bestlanguage
// s is: C++ is best language
// s is: C++ C++ is is best language



length 1.


#include
#include
using namespace std;

int main ()
{
string str = "C++ is best computer language";
cout << "str is: " << str << endl;

cout << "Length of str is : "
<< str.length() << endl;

return 0;
}
OUTPUT:
// str is: C++ is best computer language
// Length of str is : 29



max_size 1.


// returns a reverse iterator positioned
// at the last character in string
#include
#include
using namespace std;

int main ()
{
string str = "C++ is best computer language";
cout << "str is: " << str << endl;

cout << "max_size of str is: "
<< str.max_size() << endl;

return 0;
}
OUTPUT:
// str is: C++ is best computer language
// max_size of str is: 4294967294



rbegin 1.


#include
#include
using namespace std;

int main ()
{
string str = "C++ is best computer language";
cout << "str is: " << str << endl;

// usual iterator doesn't work
string::reverse_iterator It = str.rbegin();
while ( It != str.rend() )
cout << *It++;
cout << endl;

return 0;
}
OUTPUT:
// str is: C++ is best computer language
// egaugnal retupmoc tseb si ++C



replace 1.


#include
#include
using namespace std;

int main ()
{
string str = "STL is created from Dennis Ritchie";
string s1 = "was";
string s2 = "developed";
string s3 = "Stepanov alexander";
cout << "str is: " << str << endl;

cout << "replace 'is' for 'was'" << endl;
str.replace(4, // start position in str
2, // how many characters
s1); // source for replasment

cout << "str is: " << str << endl;

cout <<"replace 'created' for 'developed'" << endl;
int n = str.find('c'); // pos of 'created'
int x = str.find("from") -1;

str.replace(str.begin()+n,// start pointer
str.begin()+x, // end pointer
s2); // source

cout << "str is: " << str << endl;

cout << "replace 'Dennis' for 'alexander'" << endl;
int x1 = str.find('D'); // search Dennis
int x2 = str.find(' ',x1+1); // space after
int y1 = s3.find("alex"); // search 'alex'
int y2 = strlen("alexander");

str.replace(x1, // start position in str
x2-x1, // how characters to replace
s3, // source for replacement
y1, // start positio from source
y2); // how chracter start from y1

cout << "str is: " << str << endl;

cout << "replace 'from' for 'by'" << endl;
char ary[] = "bytes";
n = str.find("from");

// same variant possible with iterators
// instead of number of position

str.replace(n, // start position in str
4, // how many characters
ary, // source
2); // first 2 characters from source

cout << "str is: " << str << endl;

cout << "replace 'a' for 'A' (alexander)" << endl;
n = str.find("alexander");

str.replace(n, // start position in str
1, // how character(s)
1, // how many copies of character
'A'); // character for replasment
cout << "str is: " << str << endl;

cout << "replace 'Ritchie' for 'Stepanov'" << endl;
x1 = str.find('R');
y1 = s3.find(' ');

str.replace(str.begin()+x1, // start pointer
str.end(), // to the end of str
s3.begin(), // start pointer from source
s3.begin()+y1 // end pointer from
); // source
cout << "str is: " << str << endl;

return 0;
}
OUTPUT:
// str is: STL is created from Dennis Ritchie
// replace 'is' for 'was'
// str is: STL was created from Dennis Ritchie
// replace 'created' for 'developed'
// str is: STL was developed from Dennis Ritchie
// replace 'Dennis' for 'alexander'
// str is: STL was developed from alexander Ritchie
// replace 'from' for 'by'
// str is: STL was developed by alexander Ritchie
// replace 'a' for 'A' (alexander)
// str is: STL was developed by Alexander Ritchie
// replace 'Ritchie' for 'Stepanov'
// str is: STL was developed by Alexander Stepanov



reverse 1.


#include
#include
using namespace std;

int main ()
{
string str = "Anatoliy Urbanskiy";
cout << str.reverse() << endl;

return 0;
}
OUTPUT:


resize 1.


// if <=s.size(), truncates rightmost
// character in s to make it of size n; otherwise, adds
// copies of character ch to end of s to increase it size
// to n, or adds a default character value (usually a
// blank) if ch is omitted; return type is void
#include
#include
using namespace std;

int main ()
{
string str = "Alexander Stepanov";
cout << "str is: " << str << endl;
cout << "size of str is: "
<< str.size() << endl;

str.resize(11);

cout << "after str.resize(11)" << endl;
cout << "str is: " << str << endl;
cout << "size of str is: "
<< str.size() << endl;

str.resize(20,'.');

cout << "after str.resize(20,'.')" << endl;
cout << "str is: " << str << endl;
cout << "size of str is: "
<< str.size() << endl;

return 0;
}
OUTPUT:
// str is: Alexander Stepanov
// size of str is: 18
// after str.resize(11)
// str is: Alexander S
// size of str is: 11
// after str.resize(9,'.')
// str is: Alexander S.........
// size of str is: 20



rfind 1.


#include
#include
using namespace std;

int main ()
{
string str = "We go step by step to the target";
string s1 = "step";
cout << "str is: " << str << endl;
cout << "s1 is: " << s1 << endl;

cout << "int n1 = str.find(s1)" << endl;
int n1 = str.find(s1);
cout << "n1 = " << n1+1 << endl;

cout << "int n2 = str.rfind(s1)" << endl;
int n2 = str.rfind(s1);
cout << "n2 = " << n2+1 << endl;

cout << "n3 = str.rfind(s1,n2-1)" << endl;
int n3 = str.rfind(s1,n2-1);
cout << "n3 = " << n3+1 << endl;

cout << "n1 = str.rfind('t')" << endl;
n1 = str.rfind('t');
cout << "n1 = " << n1+1 << endl;

cout << "n2 = str.rfind('t',n1-1)" << endl;
n2 = str.rfind('t',n1-1);
cout << "n2 = " << n2+1 << endl;

char ch[] = "step";
cout << "char ch[] = \"step\"" << endl;
cout << "n1 = str.rfind(ch)" << endl;
n1 = str.rfind(ch);
cout << "n1 = " << n1+1 << endl;

cout << "n2 = str.rfind(\"stabc\",10,2)" << endl;
n2 = str.rfind("stabc", // pattern
10, // start position
2); // for first 2 char
// in pattern
cout << "n2 = " << n2+1 << endl;

return 0;
}
OUTPUT:
// str is: We go step by step to the target
// s1 is: step
// int n1 = str.find(s1)
// n1 = 7
// int n2 = str.rfind(s1)
// n2 = 15
// n3 = str.rfind(s1,n2-1)
// n3 = 7
// n1 = str.rfind('t')
// n1 = 32
// n2 = str.rfind('t',n1-1)
// n2 = 27
// char ch[] = "step"
// n1 = str.rfind(ch)
// n1 = 15
// n2 = str.rfind("stabc",10,2)
// n2 = 7



size 1.


#include
#include
using namespace std;

int main ()
{
string str = "We go step by step to the target";
string::size_type size = str.size();

cout << "str is: " << str << endl;
cout << "size of str = " << size << endl;

return 0;
}
OUTPUT:
// str is: We go step by step to the target
// size of str = 32



substr 1.


// str.subsr(pos,n);
// returns a copy of the substring consisting
// of n characters from str, beginning at position pos
// (default value 0); if n is too large or is omitted,
// characters are copied only until the end of s is
// reached
#include
#include
using namespace std;

int main ()
{
string str = "We go step by step to the target";
cout << "str is: " << str << endl;

int n = str.find("step");

string s = str.substr(n);
cout << "s is: " << s << endl;

s = str.substr(n,12);
cout << "s is: " << s << endl;

return 0;
}
OUTPUT:
// str is: We go step by step to the target
// s is: step by step to the target
// s is: step by step



swap 1.


#include
#include
using namespace std;

int main ()
{
string str1 = "Robert";
string str2 = "Forest";
cout << "str1 is: " << str1 << endl;
cout << "str2 is: " << str2 << endl;
cout << endl;

cout << "str1.swap(str2)" << endl;
cout << endl;

str1.swap(str2);
cout << "str1 is: " << str1 << endl;
cout << "str2 is: " << str2 << endl;

return 0;
}
OUTPUT:
// str1 is: Robert
// str2 is: Forest
//
// str1.swap(str2)
//
// str1 is: Forest
// str2 is: Robert

Wednesday, August 13, 2008

Miscellaneous cases with printf

Strings

1. printf(5+Fascimile);
O
mile
2. printf(5+"Good Morning");
O
Morning
3. printf("%c", abcdefg[4]);
O.
e