Showing posts with label string. Show all posts
Showing posts with label string. Show all posts

Wednesday

sscanf and sprintf functions

0 comments

 sscanf and sprintf functions

sscanf() function is used to extract strings from the given string.


Consider,
Char *str = "Learn C Online";
If we want to extract "Learn", "C" and "Online" in a different variable then it can be done using sscanf function.

Syntax:

sscanf(characterArray, "Conversion specifier", address of variables);

This will extract the data from the character array according to the conversion specifier and store into the respective variables.
sscanf() will read subsequent characters until a whitespace is found (whitespace characters are blank, newline and tab).

Let us understand this using an example.
char *str = "Learn C Online";
char *first, *second, *third;
sscanf(str, "%s %s %s",first,second,third);

In the above example,
"Learn" will get stored in variable first
"C" will get stored in variable second
"Online" will get stored in variable third


sprintf() function is exactly opposite to sscanf() function. Sprint() function writes the formatted text to a character array.


Syntax:

sprintf  (CharacterArray,"Conversion Specifier", variables);

Let us understand this using an example.
char *str;
char *first = "Learn", *second = "C", *third = "Online";
sprintf(str, "%s %s %s",first,second,third);

In the above example, “Learn C Online” will get stored in the character array str.
Read More ->>

String Handling Functions

0 comments

 String Handling Functions

Following are some of the useful string handling functions supported by C.
1) strlen()
2) strcpy()
3) strncpy()
4) strcat()
5) strncat()
6) strcmp()
7) strncmp()
8) strcmpi()
9) strncmpi()

These functions are defined in string.h header file. Hence you need to include this header file whenever you use these string handling functions in your program.

All these functions take either character pointer or character arrays as arguments.

strlen()
strlen() function returns the length of the string. strlen() function returns integer value.
Example:
char *str = "Learn C Online";
int strLength;
strLength = strlen(str);    //strLength contains the length of the string i.e. 14

strcpy()
strcpy() function is used to copy one string to another. The Destination_String should be a variable and Source_String can either be a string constant or a variable.
Syntax:
strcpy(Destination_String,Source_String);
Example:
char *Destination_String;
char *Source_String = "Learn C Online";
strcpy(Destination_String,Source_String);
printf("%s", Destination_String);
Output:
Learn C Online

strncpy()
strncpy() is used to copy only the left most  n characters from source to destination. The Destination_String should be a variable and Source_String can either be a string constant or a variable.
Syntax:
strncpy(Destination_String, Source_String,no_of_characters);

strcat()
strcat() is used to concatenate two strings.
The Destination_String should be a variable and Source_String can either be a string constant or a variable.
Syntax:
strcat(Destination_String, Source_String);
Example:
char *Destination_String ="Learn ";
char *Source_String = "C Online";
strcat(Destination_String, Source_String);
puts( Destination_String);
Output:
Learn C Online


strncat()
strncat() is used to concatenate only the leftmost n characters from source with the destination string.
The Destination_String should be a variable and Source_String can either be a string constant or a variable.
Syntax:
strncat(Destination_String, Source_String,no_of_characters);
Example:
char *Destination_String="Visit ";
char *Source_String = "Learn C Online is a great site";
strncat(Destination_String, Source_String,14);
puts( Destination_String);
Output:
Visit Learn C Online

strcmp()

strcmp() function is use two compare two strings. strcmp() function does a case sensitive comparison between two strings. The Destination_String and Source_String can either be a string constant or a variable.
Syntax:
int strcmp(string1, string2);

This function returns integer value after comparison.
Value returned is 0 if two strings are equal.
If the first string is alphabetically greater than the second string then, it returns a positive value.
If the first string is  alphabetically less than the second string then, it returns a negative value
 Example:
char *string1 = "Learn C Online";
char *string2 = "Learn C Online";
int ret;
ret=strcmp(string1, string2);
printf("%d",ret);
Output:
0

strncmp()

strncmp() is used to compare only left most ‘n’ characters from the strings.
Syntax:
int strncmp(string1, string2,no_of_chars);

This function returns integer value after comparison.
Value returned is 0 if left most ‘n’ characters  of two strings are equal.
If the left most ‘n’ characters of first string is alphabetically greater than the left most ‘n’ characters of second string then, it returns a positive value.
If the left most ‘n’ characters of first string is  alphabetically less than the left most ‘n’ characters of second string then, it returns a negative value
Example:
char *string1 = "Learn C Online is a great site";
char *string2 = "Learn C Online";
int ret;
ret=strncmp(string1, string2,7);
printf("%d",ret);
Output:
0

strcmpi()
strcmpi() function is use two compare two strings. strcmp() function does a case insensitive comparison between two strings. The Destination_String and Source_String can either be a string constant or a variable.
Syntax:
int strcmpi(string1, string2);

This function returns integer value after comparison.
Example:
char *string1 = “Learn C Online”;
char *string2 = “LEARN C ONLINE”;
int ret;
ret=strcmpi(string1, string2);
printf("%d",ret);
Output:
0

strncmpi()
strncmpi() is used to compare only left most ‘n’ characters from the strings. strncmpi() function does a case insensitive comparison.
Syntax:
int strncmpi(string1, string2,no_of_chars);

This function returns integer value after comparison.

Example:
char *string1 = "Learn C Online is a great site";
char *string2 = "LEARN C ONLINE";
int ret;
ret=strncmpi(string1, string2,7);
printf("%d",ret);
Output:
0
Read More ->>

Strings in C

0 comments

 Strings in C

A string is a series of characters in a group that occupy contiguous memory. A group of characters (Alphabets, digits and special characters) is called as a string.

Example 1:

“Thank you for visiting www.learnconline.com”
“www.learnconline.com is excellent site for beginners.”

A string should always be enclosed with in double quotes (").
If single quote is used then it is treated as character. Hence 'A' is different from "A". Every string ends with a null character ('\0'). Note that \0 is a single character. So when it has to be explicitly assigned to a string, it has to be written with in single quotes as '\0'.
A null character occupies 1 byte of memory.

Syntax:
char str[num_of_characters];

Example 2:
char name[20];

The above statement declares an array named “name” capable of holding 20 characters. The 20th character is null character. Hence we can store 19 characters. The 20th character is reserved for null character.

Example 3:
char name[15]=”LearnCOnline”;
Here,
‘L’ is stored at name[0]
‘e’ is stored at name[1]
‘a’ is stored at name[2]
‘r’ is stored at name[3]
.
.
.
.
‘e’ is stored at name[11]
And finally,
‘\0’ is stored at name[12].
name[14] and name[15] will contain garbage value.

Example 4:
char name[]=”Learn C Online”;
Here the size of an array is calculated automatically (15 characters).

We can also declare string as character pointer as follows:
char *name = “Learn C Online”;

Printing Strings:


Using Character pointer:
char *name = “Learn C Online”;
printf(name);
printf(“%s”, name);
printf(“%s”,&name[0]);

Using Character Array:
char name[]=”Learn C Online”;
printf(name);
printf(“%s”, name);
printf(“%s”,& name[0]);

printf function can be used to print a string. Similarly, puts function can be used to print a string as shown below.
char name[]=”Learn C Online”;
puts(name);

Accepting String as input:
We can use scanf or gets to accepts string from the user.

Using scanf:
scanf(“%s”,name);
The above statement passes the base address implicitly.

scanf(“%s”,&name[0]);
In the above statement, we are passing the address of the first element.

Using gets:
gets(name);
gets(&name[0]);

Although scanf and gets seems to be the same, there is a big difference between them. Let’s understand this difference.

Program using scanf:
char *str;
printf(“Enter the string: ”);
scanf(“%s”,str);
printf(“\n%s”,str);

Output:
Enter the string: Learn C Online
Learn

Program using gets:
char *str;
printf(“Enter the string: ”);
gets(str);
printf(“\n%s”,str);


Output:
Enter the string: Learn C Online
Learn C Online

When a string is accepted using scanf function, if a space is encountered then it will treat it as null character(‘\0’). In the above example, a space encountered after “Learn” is treated as null character. Hence while printing, only “Learn” is printed.
This problem can be resolved using gets function.
Read More ->>

Command line arguments

0 comments

Command line arguments

It is possible to pass some values from the command line to your C programs when they are executed. These values are called command line arguments and many times they are important for your program specially when you want to control your program from outside instead of hard coding those values inside the code.
The command line arguments are handled using main() function arguments where argc refers to the number of arguments passed, and argv[] is a pointer array which points to each argument passed to the program. Following is a simple example which checks if there is any argument supplied from the command line and take action accordingly:
#include <stdio.h>

int main( int argc, char *argv[] ) 
{
   if( argc == 2 )
   {
      printf("The argument supplied is %s\n", argv[1]);
   }
   else if( argc > 2 )
   {
      printf("Too many arguments supplied.\n");
   }
   else
   {
      printf("One argument expected.\n");
   }
}

When the above code is compiled and executed with a single argument, it produces the following result.
$./a.out testing
The argument supplied is testing
When the above code is compiled and executed with a two arguments, it produces the following result.
$./a.out testing1 testing2
Too many arguments supplied.
When the above code is compiled and executed without passing any argument, it produces the following result.
$./a.out

One argument expected

It should be noted that argv[0] holds the name of the program itself and argv[1] is a pointer to the first command line argument supplied, and *argv[n] is the last argument. If no arguments are supplied, argc will be one, otherwise and if you pass one argument then argc is set at 2.
You pass all the command line arguments separated by a space, but if argument itself has a space then you can pass such arguments by putting them inside double quotes "" or single quotes ''. Let us re-write above example once again where we will print program name and we also pass a command line argument by putting inside double quotes:
#include <stdio.h>

int main( int argc, char *argv[] ) 
{
   printf("Program name %s\n", argv[0]);

   if( argc == 2 )
   {
      printf("The argument supplied is %s\n", argv[1]);
   }
   else if( argc > 2 )
   {
      printf("Too many arguments supplied.\n");
   }
   else
   {
      printf("One argument expected.\n");
   }
}

When the above code is compiled and executed with a single argument separated by space but inside double quotes, it produces the following result.
$./a.out "testing1 testing2"

Progranm name ./a.out
The argument supplied is testing1 testing2
Read More ->>
 

| C programing tutorials © 2009. All Rights Reserved | Template Style by My Blogger Tricks .com | Design by Brian Gardner | Back To Top |