Jump to content
xisto Community
Sign in to follow this  
jayron

Various String Functions In C

Recommended Posts

Hi,
i read a lot of forum in which people want to learn C. Well i am giving this tutorial for all those who want to use strings. All the information provided is written by me and has not been copied from anywhere


Well First of all lets see how to store a string into a string.
First of all you need to initiate a variable which can hold an array of characters. It is necessary to initiate an array of characters rather than just a character:

for example:

char string;
is incorrect because if you store a string rather than a character in it, you cannot access each character of the string indivisually and also when you use such a data type to store different strings in a program they cause errors and also dont save them correctly.

The first thing to do is to initiate the right type of data type for it which is an array of characters. It should be initiated a follows

char string[100];
where char is the data type, string is a variable which can hold an array of characters, and 100 is the size of the array. 100 mean that the variable 'string' can store upto 100 characters.
for example. if we store the string "Jibran Bhat" in it. It would have the following values:

string[0] = Jstring[1] = istring[2] = bstring[3] = rstring[4] = astring[5] = nstring[6] =  string[7] = Bstring[8] = hstring[9] = astring[10] = tstring[11] = \0

Now lets study what values are stored in the variable. If you notice the first character of the string i.e. 'j' was stored in string[0] rather than string[1]. This is because the count for characters in an array starts from zero. This mean that even though we have initiated the string with string[100], the 100th character would be stored in string[99] and string[100] does not exist. This is an important point that should be kept in mind while programming. Often programmers encounter several bugs in their programs just because they forget that the character count in a string starts from 0.
Now, you may have also noticed that after the last character i.e. 't' stored in string[10], string[11] has the value \0. This \0 is "string terminating character" or Null. This marks the end of the string.

Now that you know the structure of a variable of "array of characters", it would be easy for you to use strings.

Now lets come to storage of strings in a variable.

To store a string in the variable string we can either use an input function such as scanf(),gets() etc or you can put a value yourself. To copy a string to a variable C provides us with a function. This function is strcpy(). its syntax is strcpy(variable, string).
For example we can store the value jibnet in the variable string with the following syntax:
strcpy(string,"jibnet");

This would store value "jibnet" in the variable string. And i hope by now you would know where would each character be stored.

For the time being i have written this much only. I will write more on all the functions and strings in an elobrate manner if you want me to.
If you want me to post more. Then please reply. I would be glad to give you all a nice tutorial on strings or any other topic in C. Although i can teach you C but i dont have any information about C++. I havent learnt C++. But its the same thing.

Anyways, reply to the post if you want me to continue writing

Share this post


Link to post
Share on other sites

This is a great tutorial jayron/jibnet, much better than copying from another source. With a post like this you deserve the credits you earn for it, unlike when it is copied from another source. This is why plagiarism is not tolerated here. Well done! :rolleyes:

Share this post


Link to post
Share on other sites

Let us study some of the string funtions provided by C. The first one i would like to focus on is strcpy();
Well its syntax as i have written before is

strcpy(destination, source);
where destination is the variable to which the string will be copied and source is the variable from which the string will be copied.
for e.g. if we initiate two string like this
char str1[100],str2[100];
this would inititate two strings str1 and str2 which can store 100 characters or elements(each character of a string is its element).
Now if we want to copy "Hello My friend" to the variable str1, this can be done with the following line of code
strcpy(str1,"Hello My friend");
this would copy the string to the variable str1.

Now if we want to store the same string in the variable str2, we can also use str1 as the source. This can be done with the following line
strcpy(str2,str1);
this would copy the string in str1 to str2.
Note that when i wrote the string manually i enclosed it with quotes("") so that the compiler treats it as a string and when i wrote str1 instead of it i didnt use the quotes("") so that the compiler treats it as a variable.

Now the next function that we come to strcat(). This function is used to append the string in one variable to the other. The syntax for the function is
strcat(str1,str2);
Where str1 is the variable to which the string is to be appened and str2 is the variable which contains the string to be appened.
Now for example we have stored "Jibran" in str1 and "Bhat" in str2. If we want to append the string in str2 i.e. "Bhat" to the string in str1 i.e. "Jibran", we should write the following line:
strcat(str1,str2);
When this is executed the string in the variable str1 would be "JibranBhat". Note that there is no space between "Jibran" and "Bhat" this is because none of the string contained an empty space. Now if we want to have the result as "Jibran Bhat" stored in the variable str1 we should write these lines of code:
strcat(str1," ");strcat(str1,str2);
This way the string in the variable str1 would be "Jibran Bhat".


Next we come to strcmp(). This function is used to compare two strings. It is very usefull while making large programs and especially when making inventory softwares. The syntax of the function is:
strcmp(str1,str2)
Where str1 and str2 are two variables which contain the strings to be compared. If the two strings are exactly same then the function returns 0 otherwise it returns any other value other than 0. To stored the value returned by the function strcmp(), we need to declare an integer first. An integer is a data type which can hold numeric data but without any decimals. So the declaraction would be as:
int result;
This would declare a variable "result" which can hold integers.
Now lets compare two strings str1 whose value is "jibran" and str2 whose value is "jibran" and store the returned value in the variable 'result'. The code would be:
result=strcmp(str1,str2);
The value returned by strcmp which would be 0(since the string in str1 and the string in str2 are same) will be stored in the variable result. Therefore result would have value 0 in it.
Now if we compare two more strings str1 whose value is "jibran" and str2 whose value is "Jibran" and store their value in result. Then the value in result would not be 0. This is because the strings stored in str1 and str2 are not same. That means "Jibran" is not same as "jibran" because characters 'J' and 'j' are different. This comparison is case sensitive.
Edited by jayron (see edit history)

Share this post


Link to post
Share on other sites

Great tutorial for C strings since these are used often in C programs and even in java some. However, for those who want to learn C++ and are willing to use the optimized code in the Standard Template Library (STL) then you also have the option of using the <string> library that has other functionality built in like the comparison operator, equality, and manipulations. A great place to see how to use these functions and such if you dont want to use C Strings is http://forums.xisto.com/no_longer_exists/ since it gives descriptions of functions and examples of those that are used the most often. Good luck.

Share this post


Link to post
Share on other sites

Very nicely done, though I have a question for anyone able to answer *nods to the admins*.Shouldn't this be in the tutorial section of Xisto?And for everyone else who uses C++, use <cstring> instead of <string>.Or if you're really adventurous, read the string.h code and recode it in C++.Saves Kilobytes.

Share this post


Link to post
Share on other sites

I have written this simple description of strings in case you cannot understand the one given at the begining.

The way a group of integers can be stored in an integer array, similarly a group of characters can be stored in a character array. Character arrays are many a time also called strings. Many languages internally treat strings, but sometimes conceal this fact from the programmer. Character arrays or strings are used by [programming languages to manipulate text such as words and sentences.

A string constant is a one-dimensional array of characters teminated by null (\0). For example,

char name[] = {H,A,E,S,L,E,R,\0};
Each character in the array occupies one byte of memory and the last character is always \0. What character is this? It looks like two characters, but it is actually only one character, with the \ indicating that what follows it is something special. \0 is called null character. Note that \0 and 0 are not same. ASCII value of \0 is 0, Whereas ASCII value of 0 is 48.The elements of the character array are stored in contiguous memory locations.
The terminal null (\0 is important, because it is the only way the functions that work with string can know where the string ends. In fact, a string not terminated by a \0 is not really a string, but merely a collection of characters.
C concedes the fact that you would use strings very often and hence provides a shortcut for initializing strings. For example, the string used above can also be initialized as,
char name[] = HEASLER;
Note that, in this declaration \0 is not necessary. C inserts the null character automatically.

Share this post


Link to post
Share on other sites
Security - string functions in cVarious String Functions In C

 These functions are inherently inscure. I wrote a small article about their safer replacements here:

Safercode.Com/blog/2008/11/04/unsafe-functions-in-c-and-their-safer-replacements-strings-part-I.html

-reply by Shantanu Goel

 

Share this post


Link to post
Share on other sites
nosebleedVarious String Functions In C

 Create a program with two character arrays string1 and string2. Your program should be able to compare the two arrays by character using the index numbers.

To compare the characters, you must create a function compareChar which takes two parameters of type char.The first parameter should correspond to a character at string1 whileThe second parameter should correspond to a character at string2 atIndex and. Your function should check if the characters at index andAre the same and must return 1 to the main function and 0 if they areNot. Once the value of compareChar is 0, it should stop comparing theRest of the characters and must output to the user the index numberWhere mismatch is found. Can you answer that asap? :(( I need it now.

-question by ethel

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.