Saturday, June 20, 2015
[Programming book CHAPTER Nine] string (string).
Bengali word meaning the string you know, they have no reason to panic, it is not hard to tug at the string programming. Those of you who may have little knowledge on the type of string theory, the term has come to them. However, there is no reason for concern.
One or more of the character string is made. In simple words, the character type array of strings. However, the use of programming so much that it is no coded string is considered to be a separate data type. However, with an array of type char string in C, we'll work on.
Thus, we can declare the string. Four declaration is actually the same thing. At the end of the Null character ('\ 0'), the compiler know where the end of the string. The third example of the separate elements of the array was written, were written together. Null character in the compiler itself will sit. The fourth example is a little strange. Here are the names that have been used to figure out the pointer (pointer). The book detailed discussion of such things, but we're not going to use it occasionally.
Notice here that printf- inside% s has been used to print the string. The array at the end of \ 0ta came when not in use. It should be understood that the present kampailaragulo.
The program for free. Then for the following program. What difference do you see in the output? What is the reason for the difference?
What's the difference whether you run it, and the computer will understand the program. Because of the differences in the programs in one place, the string '\ 0' because there is a compiler for the strinta has ended.
Then we write a program. All the characters in a string inside the capital (the capital letter or uppercase character) to be converted. However, we need to know a thing. In contrast to the computer using a number code for each character. According to the code, 'A'- of the standard 65,' B'- of the standard 66, 'C'- the standard 67 ... the' Z'- of the standard 90. So 'a' is 97, 'b' 98's ... the 'z' is 122. So any uppercase characters that we can determine whether this: if (ch> = 'A' && ch <= 'Z'), or if (ch> = 65 && ch <= 90). As for the lowercase letters: if (ch> = 'a' && ch <= 'z'), or if (ch> = 97 && ch <= 122). Now, if a character is lowercase, but the way he's capitalization? Way too easy. See an example: char ch = 'c'; ch = 'A' + (ch - 'a'); Here is his first to ch 'a' means of minus 'c' from 'a' minus (subtract 97 from the 99's). Difference 2. Now 'A'- however, let's you add the 2' C 'will be!
Uppercase conversion program is now entering a Fellow at the lowercase-. Then start reading the book again.
Notice here that the string (character array) to see how many of the elements that I wrote down and putting them straight length = 10.
Now we will write a function to measure the length of a string! It is not difficult. A loop of string to each element of the test, and Null character ('\ 0') to be taken out of the loop, ie, '\ 0' will continue until the loop. The total length of the string will be run as many times as the loop.
Programataya you see above scanf function has been used for the input and the input string is used for% s. yatati material scanf function takes as input, and returns the number. The return value is usually not necessary for us, so that the value using scanf, but we can not keep. When the input as we write two integers: scanf ("% d% d", & n1, & n2) ;. We want it this way, I could write: value = scanf ("% d% d", & n1, & n2) ;. If you see print quality of value- 2. while (1 == scanf ("% s", country)) line, which is what is happening, as long as the name of a country- being input with scanf, the function returns 1, the inner loop condition is true (1 == 1) , so the loop will continue to work.
Another thing that a notice of country- did not use any of the & symbol. What's on in the program by entering the & country. However, any whitespace string is a problem with the use of% s characters (such as spaces, tabs, etc.) can not be, if scanf something that takes a string of characters. For example, if this is the input string, the latter, however, this will be the first scanf, scanf function call is then again, it is he who will take the input string. To avoid this we can use the gets function.
This program will continue until you ctrl + z (ie z together and ctrl on your keyboard) Press, for Linux ctrl + d. ctrl + z or ctrl + d gets the function returns NULL. Another thing I noticed is that a char ara [100]; The maximum length of the string will have to be declared at the beginning of 100.
Another thing. even though not actually used inside a function of two variables string_length.
Now you do not need to be string_length function for loop while loop written using.
Our next program will aim to concatenate two strings or pairs. For example, if a string is "bangla" and another string is "desh" However, with two pairs of "bangladesh" would make.
You are not the first to declare stringulo: char str1 [] = "bangla", str2 [] = "desh", str3 [12];
Our goal is to str3 "bangladesh" hold. Some of the benefits would be if we could write:
str3 = str1 + str2;
But C can be added to the two arrays or strings. So that a copy should be str3 str1- ingredients, the ingredients of str2- str3 to copy.
The program for free. What should be the output? bangladesh. But the output is desh. In fact, we've made a mistake. Now you should try to fix the error. Despite the efforts of at least thirty minutes, if you can not make a mistake to start reading the book again.
Here we have the beginning, because i i- value of 0 to the index as we will use str1-. Who will use the index as str3- j j- of the standard is 0. Str1- copy of each of the ingredients are then str3 and the value of 1 if i and j- baracchi (i ++, j ++). After the end of the loop the value of i and j 6.
Now the loop, we'll str2 a copy str3-. Now if i use str2- as the index, but its value at the beginning of the loop to be 0 again. We have it. But that's wrong with the j- value of 0. If the value of j- 0 str2- of the first (0-th) element of str3- first (0 th) as the material to be copied, but we do not want it. We want to be the first element of str2- seventh element of str3-. J- If the value can not be 0.
Notice another thing. After being out of the loop end of the second chamber str3- '\ 0''ve assigned (str3 [j] = '\ 0';) so that strinta end there, the compiler can not understand it.
Our next program will be compared between the two strings. That is, in the string of small, large, equal to diagnose. As in the case of>, <,> =, <=, == keys can be compared, in the case of the string is not in the system. But this is often the case, the string will need to be compared. Just as, in the case where the sorting of small to large or small in order to be sort (alphabetical ordering). What's the string of large and small? More than words can not explain why some examples, then you will understand. 'Aaa'- than' aab 'big. The 'ba' and 'ca'- the' ca 'big. In this program, we will write a function string_compare (), which would be a comparison between the two string will return 1 if the first one is bigger than the second, smaller will return -1, and 0 if both are equal. The return type of the function parameters will be two integers and an array of type char.
The first elements of our work a- b- of the first element, a- b- with the second component of the second material can be compared to. Whenever any component of a- b- to be smaller than any of the ingredients, we can say that with a, b- smaller. So come out from the function returns -1. Similarly, when any element of a- b- to be larger than any of the material, with 1 out of the function will return the a, b- greater. But all of them are equal? Then we'll return 0. I know that the two strings are equal.
Basic string discuss things. Interestingly, however, a header files are coded in C, whose name string.h and over most of the string-related tasks have been made for the function (eg: strcmp, strlen, strcpy, etc.). I work with you, to be made again, there is no reason to be sad, do not be angry with me. It's always important to keep learning the fundamentals, which will develop your programming ideas.
Now we will write another program which will take as input a string (where there will be many words). 1000 will be the maximum length of the string. There will be one or more spaces between words. The output of each word as a separate line to be printed. Biramacihnagulo (punctuation) can not be printed and will be uppercase first letter of the word.
Wipe with a lot of conditions. The program is very difficult. You can try yourself. Let's see now how it could not be resolved.
The first thing, how do I? Do you know the space that will be input, scanf ("% s") can not be used. So we gets () will use. Being next to no character can have a word? As is known, we assume 'a' to 'z', 'A' to 'Z' and '0' to '9' will.
The next question is, we tell when or how we'll reason that the program has been a sound? -For A variable we can. If the value is 0, then the name of the variable is_word_started do not start unnecessary words, when the sound of the standard, we will 1. Will sound again at the end of 0. When we see the word has begun (is_word_started- value of 1) the value of any of the characters 'a' - 'z' or 'A' - 'Z', or '0' - '9' is not in this range, then tell the end of term . Before you write, do not even try to program, so if you hope to be. I'll write the code to see if it will have to try it yourself before.
Understand what the program is having a little trouble? It can be viewed later, even if the type of the computer immediately before the program, compile and execute. They are people who use Linux gets () the use of a warning from the compiler (warning) may have, and do not care.
You can write anything as input. For example: This is a test .. What is the output?
The output looks like this:
This
Is
A
What is difficult! Where is the test?
Now your job is to investigate the mystery of the disappearance of test-. Then I'll explain the program.
Strlen function for determining the length of the string you see in the program I've used. And for me to have to include the header file string.h. S- strinta as input in the auction. We have to declare an array of the word. Then I ran s- i = 0 to the length of the inside of a loop to check each and every character.
if (s [i]> = 'a' && s [i] <= 'z') or with a lowercase tried it. However, the first letter of a word in lowercase letters to find out what it will do. If the first character to be down again capitalization. We did the test: if (is_word_started == 0) with. This is the true meaning of the word has not started, the first letter. So we will is_word_started- value of 1. The word [j] to s [i] - take the uppercase letter. Then one will have to increase the value of j-. else if (s [i]> = 'A' && s [i] <= 'Z') and else if (s [i]> = '0' && s [i] <= '9') of these two conditions We work within the same. s [i] the word [j] to make copies. So if you could write the two terms together: else if ((s [i]> = 'A' && s [i] <= 'Z') || (s [i]> = '0' && s [i] <= '9')), enter the following means of else- if and else if- the criteria had been lying. So s [i] - the inside of the characters can not be left in the word-. And if the word has already begun, it will end and the printed word to be. If this is not the start of a word does not need to do anything.
Test- Did you solve the mystery? If you want to try a different way, and I now write the program (it has nothing to do with the test of mysticism told).
I'll do for now, as I write the program so that it does not use the word ayaretii! Look a little thought. So we're not actually in the array without print. So there's really no need.
Now try to understand the program and examine the various inputs. For example:
This is test number 9.9
The string-related problems seem complicated, but in fact easy to see. He will practice skills such as problem-solving will be more.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment