Turbo C/C++ For Windows 7/8/8.1 and 10 32/64 Bit : Codeplex
Turbo C/C++ For Windows 7/8/8.1 and 10 32/64 Bit : Codeplex
Turbo C/C++ For Windows 7/8/8.1 and 10 32/64 Bit : Codeplex
Register
Sign In
SOURCE CODE
Page Info
DOWNLOADS
DOCUMENTATION
DISCUSSIONS
What is a String?
Note that along with Cstyle strings, which are arrays, there are also string literals, such as "this". In reality, both
of these string types are merely just collections of characters sitting next to each other in memory. The only
difference is that you cannot modify string literals, whereas you can modify arrays. Functions that take a Cstyle
string will be just as happy to accept string literals unless they modify the string in which case your program will
crash. Some things that might look like strings are not strings; in particular, a character enclosed in single
quotes, like this, 'a', is not a string. It's a single character, which can be assigned to a specific location in a string,
but which cannot be treated as a string. Remember how arrays act like pointers when passed into functions?
Characters don't, so if you pass a single character into a function, it won't work; the function is expecting a char*,
not a char.
ISSUES
PEOPLE
Follow 1
SYSTEM REQUIREMENTS
"Thisisastaticstring"
charstring[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, we've accounted for the extra with a null character, literally a '\0' character. It's
important to remember that there will be an extra character on the end on a string, just like there is always a
period at the end of a sentence. Since this string terminator is unprintable, 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.
Note that something like
char*my_string;
can also be used as a string. If you have read the tutorial on pointers, you can do something such as:
arry=malloc(sizeof(*arry)*256);
which allows you to access arry just as if it were an array. To free the memory you allocated, just use free:
For example:
free(arry);
Using Strings
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 scanf to input a string works, but it will terminate the string after it reads the first space, and
moreover, because scanf doesn't know how big the array is, it can lead to "buffer overflows" when the user inputs
Subscribe
To recap: strings are arrays of chars. String literals are words surrounded by double quotation marks.
Remember that special sauce mentioned above? Well, it turns out that Cstyle strings are always terminated with
a null character, literally a '\0' character with the value of 0, so to declare a string of 49 letters, you need to
account for it by adding an extra character, so you would want to say:
LICENSE
a string that is longer than the size of the string which acts as an input "buffer".
There are several approaches to handling this problem, but probably the simplest and safest is to use the fgets
function, which is declared in stdio.h.
The prototype for the fgets function is:
char*fgets(char*str,intsize,FILE*file);
There are a few new things here. First of all, let's clear up the questions about that funky FILE* pointer. The reason
this exists is because fgets is supposed to be able to read from any file on disk, not just from the user's keyboard
or other "standard input" device. For the time being, whenever we call fgets, we'll just pass in a variable called
stdin, defined in stdio.h, which refers to "standardinput". This effectively tells the program to read from the
keyboard. The other two arguments to fgets, str and size, are simply the place to store the data read from the
input and the size of the char*, str. Finally, fgets returns str whenever it successfully read from the input.
When fgets actually reads input from the user, it will read up to size 1 characters and then place the null
terminator after the last character it read. fgets will read input until it either has no more room to store the data
or until the user hits enter. Notice that fgets may fill up the entire space allocated for str, but it will never return a
nonnull terminated string to you.
Let's look at an example of using fgets, and then we'll talk about some pitfalls to watch out for.
For a example:
#include<stdio.h>
intmain()
{
/*Anicelongstring*/
charstring[256];
printf("Pleaseenteralongstring:");
/*noticestdinbeingpassedin*/
fgets(string,256,stdin);
printf("Youenteredaverylongstring,%s",string);
getchar();
}
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 addresses, so the values in the array string are modified.
The one thing to watch out for when using fgets is that it will include the newline character '\n' when it reads
input unless there isn't room in the string to store it. This means that you may need to manually remove the
input. One way to do this would be to search the string for a newline and then replace it with the null terminator.
What would this look like? See if you can figure out a way to do it before looking below:
charinput[256];
inti;
fgets(input,256,stdin);
for(i=0;i<256;i++)
{
if(input[i]=='\n')
{
input[i]='\0';
break;
}
}
Here, we just loop through the input until we come to a newline, and when we do, we replace it with the null
terminator. Notice that if the input is less than 256 characters long, the user must have hit enter, which would
have included the newline character in the string!
fgets(name,50,stdin);
/*seedefinitionabove*/
strip_newline(name,50);
/*strcmpreturnszerowhenthetwostringsareequal*/
if(strcmp(name,"Alex")==0)
{
printf("That'smynametoo.\n");
}
else
{
printf("That'snotmyname.\n");
}
//Findthelengthofyourname
printf("Yournameis%dletterslong",strlen(name));
printf("Enteryourlastname:");
fgets(lastname,50,stdin);
strip_newline(lastname,50);
fullname[0]='\0';
/*strcatwilllookforthe\0andaddthesecondstringstartingat
thatlocation*/
strcat(fullname,name);/*Copynameintofullname*/
strcat(fullname,"");/*Separatethenamesbyaspace*/
strcat(fullname,lastname);/*Copylastnameontotheendoffullname*/
printf("Yourfullnameis%s\n",fullname);
getchar();
return0;
}
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 beena
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,constchar*src,size_tlen);
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.
Last edited Sep 2 at 4:54 AM by vineetchoudhary, version 1
20062015 Microsoft
Get Help
Privacy Statement
Terms of Use
Code of Conduct
Advertise With Us
Version 8.21.2015.21031