Skip to main content

Interesting Inferences about strings in C++ (and related library functions)

1. strlen() FUNCTION OF string.h

It stops counting where it finds uninitialised character (ie if Arr[i] is uninitialised, it gives length as i)
for eg. char S[10];
S[0]='c';
cout<<strlen(S); //This prints '1'
for eg. char S[10];
S[4]='c';
cout<<strlen(S); //This prints '0', even though there is one character

NOTE- Though such case may not be ever encountered in actual use but i just found this out experimenting with the code

Reason- See the function definition of strlen() in string (or string.h on older ones) header file

2. THE NULL CHARACTER

The null character (that terminates strings, ie '\0') has the integer code '0'.
(i. cout<<int('\0'); gives 0)
'\n' has code 10
' ' has code 32
txt files terminate with a character with code '-1', but when i tried to print the respective character using cout<<char(-1); it just printed a space!?

//I used Dev C++ Ver 5.11 (compiler)

Comments

Popular posts from this blog

Using 'ls' command as a replacement of 'find'

Advantage over find - Actually not probably in terms of speed, but... only point of this is that it's an observation, but... you can do like 'search only in' */*/*Programs* , /*/*Programs*/* , this may compensate the extra time took by using two commands 1. To search in current folder only (1 level... only this folder, no subfolder)     Type... ls | grep Search_Term 2. To search in current folder only (till level 2... this folder + subfolder)     Type... ls * | grep Search_Term #ls and ls * are NOT same! 3. To search in current folder only (till level 3... this folder + subfolder + subfolder)     Type... ls */* | grep Search_Term 4. To search in current folder only (till level 4... this folder + subfolder + subfolder + subfolder)     Type... ls */*/* | grep Search_Term ... and so on... FORMULA - To search till 'n' level (considering current folder as 1)... go till (n-1) stars If wanting to search whole disk... better type star...

LudO - The Game

/* Programmer - Aditya Gupta (Techy15) Language - C++ Program - LudO - The Game */ /* PLANS- Add functionality for name of player */ //IMP NOTE - See all notes, written in this form "//NOTE..." //NOTE- Mark 'R','G','B','Y' for gotis, ignore doubling for now //NOTE- Settings will have options: change default game play order(ie RBYG), change/give names to each colour //NOTE- Add code in case of attacks //NOTE- Add stops, and ways to show them distinct from others {may require graphics.h} #include<iostream> /* #include<fstream>    To be used in case save & resume to be used*/ #include<cstdio> #include<cstdlib> using namespace std; //Changes on 16th Oct //Show gotis as R1, R2... and ask user to enter string R1 or whatever // // Place within appropriate block ...     short diethrow(){     int fl=1; //holds 0 if decnum is 0     while(fl==0) { char *temp;     ...

Most Code will now be at my GitHub - github.com/adi-g15

Friends... i didn't update this blog for a month. But actually it was the period of time, i LEARNED and DID the most. The sole purpose of this blog for me, was just to put 'Interesting Observations' i used to get during finding or solving the errors, and some just clicked to try. Now, that i have actually been trying many languages and design, so most of the new code part will be on GitHub from now on... Thanks for reading... and I WILL keep posting more inferences here (I got dozens in this month due to the projects ;D ). And special thanks to DevHack1.0 team! GitHub link -> https://github.com/AdityaGupta150