Pages

Thursday, November 25, 2010

Advanced Strings

Let's make some more programs using character pointers.Don't forget that strings are arrays of characters. Read the description at the end to understand the code.

Example 1 - To copy a string to another

#include
#include
void main()
{
    clrscr();
    void copy(char *);
    char str1[10],str2[10];
    printf("\nEnter a string  : ");
    scanf("%s",&str1);
    copy(str1);
}
void copy(char *s)
{
    int i=0;                     // Line  1
    char str2[10];         // Line 2
    while(*s!='\0')         // Line 3
    {                               // Line 4
        str2[i++]=*s;     // Line 5
        s++;                   // Line 6
    }                           // Line 7
    str2[i]='\0';         // Line 8   
    printf("\nStr2 %s",str2);    // Line 9
}


Description

We ask the user to enter a string and place it in the string "str1". While calling the function "copy", we passed the value of "str1" to the argument "s". Inside the function we have another string2 which we will use as the target string. Let's see how the source("str1") is copied to the target("str2"). In the example we are assume that the user has entered the word "hello".


Line 1-> str1="hello"  is copied to the argument "s" in the function copy()
Line 2 -> str2=NULL
Line 3 -> *s="h" (s points to the first character of hello)
since *s is not equal to '\0, the compiler enters the loop.
Line 5 -> The first index of str2 which is 0 is assigned the value at "s" which is "h".
Line 6 -> The value of "s" is incremented so that it now points to the next char which is "e".
The loop continues till the value at "s" is not equal to '\0', which is the end of the string.
Line 8 -> After all the characters have been copied to to "str2", the null character "\0" is appended  to the end of the string. This will make "str2" a string which terminates with "\0"
Line 9 -> Once the string is copied,it is printed.
                       
Since parameters are passed by value, in both the passing of a character pointer or the name of the array as above, what actually gets passed is the address of the first element of each array. Thus, the numerical value of the parameter passed is the same whether we use a character pointer or an array name as a parameter. 
Another way to traverse the string would be to change:
while (*source != '\0')
to simply
while (*source)
since the value within the parenthesis will go to zero (FALSE) at the same time in either
case.

Example 2  - To find out the length of a string without using the strlen()

#include
#include
void main()
{
    clrscr();
    int len=0;
    char *text;
    printf("\nEnter a sentence  : ");
    gets(text);
    while(*text!='\0')
    {
        len++;
        text++;
    }

    printf("\nLength of the string is %d",len);
}


Description
The above program is very simple. It prompts the user to enter a sentence. Note that the function to get the string from the user is "gets()" and not "scanf()". This is because we want to allow the user to enter spaces in between the string. You can replace the "gets()" with "scanf()" if required.  To find the length of the string, we are running a loop which runs till the end of the string.On each run of the loop the value of "len" is incremented by 1. When the loop closes, the value of length will be printed.

0 comments:

Post a Comment