String

 

Strings

string is a sequence of characters terminated with a null character \0.

The C compiler automatically places the '\0' at the end of the string when it initializes the array

Eg.

char c [] = "c string";

char c[] = "abcd";

char c[50] = "abcd";

char c[] = {'a', 'b', 'c', 'd', '\0'};

char c[5] = {'a', 'b', 'c', 'd', '\0'};

Read String from the user

The scanf() function reads the sequence of characters until it encounters whitespace (space, newline, tab, etc.)

Eg.

#include <stdio.h>

int main()

{

    char name[20];

    printf("Enter name: ");

    scanf("%s", name);

    printf("Your name is %s.", name);

    return 0;

}

  How to read a line of text?

 gets() and puts()-Functions gets() and puts() are two string functions to take string input from the user and display it . fgets() function to read a line of string. And,  puts() to display the string.

 Example1

 #include<stdio.h>

 int main()

{

    char name[30];

    printf("Enter name: ");

    gets(name);     //Function to read string from user.

    printf("Name: ");

    puts(name);    //Function to display string.

    return 0;

}

  Example 2: fgets() and puts()

#include <stdio.h>

int main()

{

    char name[30];

    printf("Enter name: ");

    fgets(name, sizeof(name), stdin);  // read string

    printf("Name: ");

    puts(name);    // display string

    return 0;

}

fgets(name, sizeof(name), stdlin); // read string

The sizeof(name) results to 30. Hence, we can take a maximum of 30 characters as input which is the size of the name string.

To print the string, we have used puts (name);

 

String Functions

Function

Work of Function

strlen()

computes string's length

strcpy()

copies a string to another

strcat()

concatenates(joins) two strings

strcmp()

compares two strings

strlwr()

converts string to lowercase

strupr()

converts string to uppercase

 

The following example uses some of the above-mentioned functions –

#include <stdio.h>

#include <string.h>

 int main ()

 {

    char str1[12] = "Hello";

   char str2[12] = "World";

   char str3[12];

   int  len ;

    /* copy str1 into str3 */

   strcpy(str3, str1);

   printf("strcpy( str3, str1) :  %s\n", str3 );

    /* concatenates str1 and str2 */

   strcat( str1, str2);

   printf("strcat( str1, str2):   %s\n", str1 );

    /* total lenghth of str1 after concatenation */

   len = strlen(str1);

   printf("strlen(str1) :  %d\n", len );

    return 0;

}

 

Strings and Pointers

we can use pointers to manipulate elements of the string.

 Example

 #include <stdio.h>

 int main(void)

{

  char name[] = "Harry Potter";

   printf("%c", *name);     // Output: H

  printf("%c", *(name+1));   // Output: a

  printf("%c", *(name+7));   // Output: o

   char *namePtr;

   namePtr = name;

  printf("%c", *namePtr);     // Output: H

  printf("%c", *(namePtr+1));   // Output: a

  printf("%c", *(namePtr+7));   // Output: o

}

 String User Defined operation-

 The programs under this section are not using any string.h header file's library function.

Array of String

An array itself defines as a list of strings. The string is a collection of characters, an array of a string is an array of arrays of characters. An array of a string is one of the most common applications of two-dimensional arrays.

 syntax for array of string 

datatypename_of_the_array[size_of_elements_in_arra];

 

examples

char str_name[size];

char str_name[8] = "Strings";

char str_arr[2][6] ={"gouri", "ram"};

char str_name[8] = {'s','t','r','i','n','g','s','\0'};

Examples of Array String

#include <stdio.h>

int main()

{

char name[10];

printf("Enter the name: ");

fgets(name, sizeof(name), stdin);

printf("Name is : ");

puts(name);

return 0;

}

 

Command Line Arguments


Command-line arguments are simple parameters that are given on the system's command line, and the values of these arguments are passed on to your program during program execution.

  • When the main function of a program contains arguments, then these arguments are known as Command Line Arguments.
  • The main function can be created with two methods: first with no parameters (void) and second with two parameters.
  • The parameters are argc and argv, where argc is an integer and the argv is a list of command line arguments.
  • argc denotes the number of arguments given, while argv[] is a pointer array pointing to each parameter passed to the program.
  • If no argument is given, the value of argc will be 1.
  • The value of argc should be non-negative.

 

  • argc (ARGument Count) denotes the number of arguments to be passed and
  • argv [ ] (ARGument Vector) denotes a pointer array pointing to every argument that has been passed to your program.

Eg.

 #include <stdio.h>

 int main( int argc, char *argv [] )

{

    printf(" \n Name of my Program %s \t", argv[0]);

 

    if( argc == 2 )

    {

        printf("\n Value given by user is: %s \t", argv[1]);

    }

    else if( argc > 2 )

    {

        printf("\n Many values given by users.\n");

    }

    else

    {

        printf(" \n Single value expected.\n");

    }

}

 

SHARE

Milan Tomic

Hi. I’m Designer of Blog Magic. I’m CEO/Founder of ThemeXpose. I’m Creative Art Director, Web Designer, UI/UX Designer, Interaction Designer, Industrial Designer, Web Developer, Business Enthusiast, StartUp Enthusiast, Speaker, Writer and Photographer. Inspired to make things looks better.

  • Image
  • Image
  • Image
  • Image
  • Image
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment

Problems Classification

  Problems Classification Problem classification is the process of assigning a level, based on predefined criteria. Problem classification h...