Tuesday, 11 June 2013

Pointers, Strings & Arrays. Count the characters of a user entered string

Day before, I went down and bought the 12th edition of 'Let US C' (2012), It's a big step up from the 4th edition.
The Graphics chapter , thankfully, has been updated to Windows graphics from DOS Graphics in the 4th edition.
I'm really having a difficult time understanding strings and arrays. Okay, I do understand Arrays, they are pretty easy, but referring to them with pointers, especially in the 'String' arrays....it's a pain.
I'm not moving on to the other chapters until I have grasped the concepts- at least to the point that I can manipulate variables across functions as easily as I manipulate variables within one function by direct reference.
Say we have a char 'name[20]' that holds 'John Smith'. I want to be able to send that to another function by using a pointer, and manipulate it to change it to say: Jane Smith. I'll stick to this chapter and write small programs that do these things until it becomes second nature to me.

Here is a program that uses a function to count the length of a string the user has entered. I try writing this without looking at the book and sites, but eventually end up having to take a peek to get rid of the errors.

Suppose the user enters "My String", it is stored in an array M|Y|<space>|S|T|R|I|N|G|\0|. The \0 is called a NULL character that lets the program know that we have reached the end of the string.

So, every character the user has entered has three characteristics:

User String            Address (Will be in sequence)   Name of the Location
User may enter       I am beginning from 1000          We declared arr1
'My String'              for illustration

M                            1000                                            arr1[0]
y                             1001                                            arr1[1]
<space>                   1002                                            arr1[2]
S                             1003                                            arr1[3]
t                              1004                                             arr1[4]
r                              1005                                             arr1[5]
i                              1006                                             arr1[6]
n                             1007                                             arr1[7]
g                             1008                                             arr1[8]
\0 (NULL)                1009                                              arr1[9]

What the program does is it sends the address of the first character (1000) of the user entered string (M in this case) to the function passed():
                               int length=passed(arr1);

and it goes through each character until it encounters the '\0'.
While(the value at the address stored in x is not \0, increment the address at x)
                               while(*x!='\0') 
                               {
                                     x++;
Every character it goes through, the int variable 'y' which has been declared as a zero is also incremented by 1. y is counting the number of characters we are going through:
                                 y++;
                                 }
 Finally 'y' (number of characters we went through before we reached \0) is sent back to main:
                                 return(y);

 which stores it in 'length' and is displayed with printf.
                                 int length=passed(arr1);

                                 printf("Length of:\n%s= %i",arr1,length);

Characters in a string can be counted by using the standard library string function strlen() (string length), but this program tries to emulate what strlen() does, and if I figure this out, I feel I will have a better understanding about pointers and related arrays and variables.

Here is the program:

#include <stdio.h>

int passed(char *x);

main()
{
    char arr1[500];
    printf("Enter a string to count it's length:\n");
    gets(arr1);
    int length=passed(arr1);

    printf("Length of:\n%s= %i",arr1,length);
}

int passed(char *x)
{
    int y=0;
    while(*x!='\0')
    {
        x++;
        y++;
    }
    return(y);
}
__________________________________________________________________________

Here is a sample output:

Enter a string to count it's length:
My String             //User enters this
Length of:
My String= 9



No comments:

Post a Comment