Skip to main content

Posts

Showing posts with the label Strings

Command Line Arguments

So far we have using main with an empty pair of brackets. But like any other function it is possible to pass argument or parameters to main when it starts executing i.e. at runtime. These arguments are called command line arguments because they are passed to main() from the command line during execution.  main(int argc, char * argv[])  { -------  -------  }   int argc: Argument count which is the number of command line arguments that are passed to function main().    char *argv[]: argument vector it is an array of pointer each pointing to a command line argument.    i. The subscripts for argv[] are 0 to argc – 1.    ii. argv[0] is the name of the executable file of the program (./a.out). It is not necessary to use the words argc and argv. Any other identifiers can be used. The arguments can have to be separated by white spaces.   Example : Consider a program“Display.c” which displays the command line arguments on the ...

User Defined Functions for Predefined Functions

The above string functions can be written as user defined functions. In this section, some of the user defined functions are given. These functions can be called in the same way as predefined functions.   User defined function for strlen() We increment the pointer till reaches ‘\0’ and use a counter to count.    int slen(char*s)   {   int count=0;   while (*s!=’\0’)   {   count++;   s++;   }   return count;   }   void main()   {   char str[80];   gets(str);   printf(“The length is %d”,slen(str));   }    User defined function for strcpy()  In this function, we copy each character from the source string to the target string using pointers and return the target string. char * scpy(char * target, char * source)    {    char *t=target;    while(*source !=‘\0’)   ...

Predefined String Library Functions

Following are the functions defined in the file string.h   char *strcat(char *dest, char *src)  Appends the string pointed to, by src to the end of the string pointed to by dest.    char *strncat(char *dest, char *src, int n) Appends the string pointed to, by src to the end of the string pointed to, by dest up to n characters long.  char *strchr(char *str, char c) Searches for the first occurrence of the character c in the string pointed to, by the argument str.  int strcmp(char *str1, char *str2) Compares the string pointed to, by str1 to the string pointed to by str2.  int strncmp(char *str1, char *str2, int n) Compares at most the first n bytes of str1 and str2.   char *strlwr(char *str)   Converts a string pointed to by str to lowercase.    char *strcpy(char *dest, char *src) Copies the string pointed to, by src to dest.  char *strncpy(char *dest, char *src, int n) Copies up to n characters from the string pointed...

Array of Pointers

In the above example , each string is of a fixed size of 20 characters . However, the size of each name may be different. In this case, we can use dynamic memory allocation to allocate memory separately for each string.   If we use dynamic memory allocation for one string, we require one pointer.   char *ptr; //one pointer   ptr=(char *) malloc(20*sizeof(char)); //one string of 20 chars Since We have many string, we need many pointers. Hence, we must use an array of pointers, each pointer pointing to a string.  The following diagram shows 10 pointers, each pointer pointing to one string. In following example, we shall see how a list of ‘n’names can be stored as an array of pointers. Currently, we will allocate memory for ‘n’names, and allocate 10 characters for each name.  Array of Character Pointers Example #include< stdio.h>  #include<string.h>  main()  { char *ptr[20];  int i,n;  printf(“\nHow many names?”)...

Array of Strings

Sometimes useful to have an array of string values.  An array of strings is a two dimensional array of characters. This is often required in applications with a list of names, etc.  Example: char cities[4];[10]={“Pune”,“Mumbai”,“Delhi”,“Chennai”};  Each string could be of different length.   Example: char *MonthNames[13]; /* an array of 13 strings */   MonthNames[1] = “January”; /* String with 8 chars */   MonthNames[2] = “February”; /* String with 9 chars */   MonthNames[3] = “March”; /* String with 6 chars */  Array of Strings Example    The following program illustrates how we can accept ’n’ strings and store them in an  array of strings . we will accept the names of ‘n’ students and display them.    #include< stdio.h>    #include<string.h>    main()    { char list[10][20];    int i,n;    printf(“\nHow many names?”);    scanf(“%d...

Strings and pointers

In the previous chapter, we have seen how pointer and arrays are related. Strings and pointers are also very closely related. Since a string is an array, the name of the string is a constant pointer to the string . This pointer stores the base address of the string and can be used to perform operation on the string.  Example: Display characters of string using array   char str[20]=”Sample Message”;   for(i=0; str[i]!=‘\0’;i++)   printf(“%c”, str[i]);   Example: Display characters of string using pointer   char str[20]=”Sample Message”;   char *ptr=str;   for(i=0; *ptr!=‘\0’;i++)   printf(“%c”,*(ptr+i)); //or ptr++  Importance of NULL character   As seen above, every string has a special NULL character at the end. The importance of the character is to indicate the end of a string. Consider the for loop given below:   for(i=0; *ptr!=‘\0’;i++)   printf(“%c”,*(ptr+i));...

Strings input and output

The following functions are used for string input and output    printf() with format specifier %s    scanf() with format specifier %s    gets()     puts()      We have seen the use of printf and scanf for accepting and displaying integer, character etc. data types. These two functions can be with string using the format specifier %s.      Apart from these functions, there are two special functions for accepting and displaying strings they are gets() and puts().      The difference between scanf and gets function is that gets does not require format specifier and scanf does not allow a string with spaces. gets() allows a strings to contain spaces.     String Input      Use %s field specification in scanf to read string ignore leading white space       reads characters until next white space encountered C stores null (\0) char after last non-white space ...

Declaring and initializing string variables

single character value (stored in 1 byte) as the ASCII value for a.  “a” is an array with two characters, the first is ‘a’, the second is the character value ‘\0’.  String literal is an array, can refer to a single character from the literal as a character.    Example:     printf(“%c”, ”hello”[1]);    outputs the character ‘e’     Examples of strings literals and strings variables: “hello” //string literal     “Pune and Mumbai” //string literal     char name[20]; //string variable Initializing String Variables      Allocate an array of a size large enough to hold the string (plus 1 extra value for the delimiter)      Examples (with initialization):      char str1[6] = “Hello”;      char str2[] = “Hello”;      char *str3 = “Hello”;      char str4[6] = {‘H’,’e’,’l’,’l’,‘o’ ’\0’};       The compile...