Skip to main content

C++

C++ String Functions


As you have learned in chapter 13, strlen, strcat, strcmp are all string functions of C++. Two similar string functions are the strncat, and strncmp. These work the same as strcat, and strcmp except they allow more control over how the function operates. Strncat allow the programmer to specify the number of characters of the source string that will be appended onto the destination string. It is used as follows:
strncat(destination, source, number of characters);
Example:
char FirstName[15] = "John ";
char LastName[15] = "Smith";

strncat(FirstName, LastName, 1);

count << Firstname;
Output:
        John S
The strncmp, and strnicmp allow the comparison of only the first number of specified characters to be compared. The only difference between the two is strnicmp is not case sensitive. It is used as follows:
strncmp(string1, string2, number of characters);
Example:
char FirstName1[15] = "Rob";
char FirstName2[15] = "Robert";

test = strncmp(FirstName, LastName, 3);
cout << test;
Output:
0
Many more string functions are available in C++. Some of these functions and examples of each are shown below. You should review your compiler's documentation before using these functions in order to avoid unexpected results.
_strlwr
Converts all characters to lower case.
_strlwr(string);
_strupr
Converts all characters to upper case.
_strupr(string);
strrev
Reverses the string.
strrev(string);
strchr
Locates the first occurrence of a character in a string.
CharPointer = strchr(string, 'char');
strrchr
Locates the last occurrence of a character in a string.
CharPointer = strrchr(string, 'char');
strspn
Returns the number of matches found in the string.
Count = strspn(string, 'char');
strpbrk
Returns a character pointer to the first occurrence of characters in a string. It will return the pointer of which ever character comes first.
CharPointer = strpbrk(string, "chars")
strstr
Returns a character pointer to the first occurrence of a string in a string.
CharPointer = strstr(string, "string");


Comments

Popular posts from this blog

Login and Registration Example in JSP with Session

Those who want to start with jsp and MySQL, this is an excellent example for themselves. Here you can learn how to insert data to MySQL using JSP. Also you can learn about session handling in jsp. 1 2 3 4 5 6 7 8 9 10 CREATE TABLE `members` (    `id` int (10) unsigned NOT NULL auto_increment,    `first_name` varchar (45) NOT NULL ,    `last_name` varchar (45) NOT NULL ,    `email` varchar (45) NOT NULL ,    `uname` varchar (45) NOT NULL ,    `pass` varchar (45) NOT NULL ,    `regdate` date NOT NULL ,    PRIMARY KEY   (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1; index.jsp 1 2 3 4 5 6 ...

Timer funcitons in JavaScript: Reminder Script

Timers are used in web pages and we will discuss how to setup one and how to use one with an example. The function setTimeout() in function is used and here is its general syntax. mytime = setTimeout(expression, msec); mytime  is the identifier used to identify the current timeout function.  expression  is the statement that is to be executed after the specified time has ticked off.  msec  is the duration of time in milliseconds after which the expression will be executed.  You can see by using setTimeout function we can execute any function or object after a set of time. For example if msec is set 5000 then the expression will be executed after 5 seconds or 5000 milliseconds.  We will try one example where we will have four period buttons and each button will set a different time for another function to execute and display a alert button. We will call it as a reminder script and we will get one alert box based on the period button we click...

Binary Addition

/* File Name : BinAdd.java */    import java.util.*; public class BinAdd    {  public static String addBit(String a, String b, String c)  { String r=""; if(a.equals("1") && b.equals("0") || a.equals("0") && b.equals("1")) { if( c.equals("0")) r="1"; else { r="0"; c="1"; } } else if( a.equals("0") && b.equals("0") ) { if(c.equals("0")) r="0"; else r="1"; } else if( a.equals("1") && b.equals("1") ) { if(c.equals("0")){ r="0"; c="1"; } else { r="1"; c="1"; } } return c+r; }   public static String add(String a, String b)   { String r=""; int len=a.length(); String carry="0"; for(int i=len-1;i...

Real time changing Clock showing date and time

We can display a clock which will be showing the local time of the client computer by using JavaScript. Note that this time displayed is taken from user computer and not from the server.  We have seen in our  date object example how to display current date and time   where the current date and time is displayed once. Here we will try to display changing clock or real time change in date and time by using  setTimeout function . This setTimeout function we have used to trigger the time display function in a refresh rate of 1000 mill seconds ( 1 Sec ). This refresh rate can be changed to any other value. By changing the value to 5000 the clock will refresh and show the exact time once in every 5 seconds.  Here is the demo of this script and code is given below that.  Sat Apr 23 2016 08:27:22 GMT+0530 (IST)   Here is the code <html> <head> <title>(Type a title for your page here)</title> <script type="text/javascript...