Nothing Special   »   [go: up one dir, main page]

Vignan CP UNIT-4 Strings

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 33

1

UNIT-4
STRINGS
INTRODUCTION: - In ‘C' language the group of characters, digits, and symbols enclosed
within quotation marks are called as string. The string is always declared as character arrays.

Every string is terminated with’\0’ (NULL) character the NULL character is a byte with all bits
at logic zero.

EX:-

char name [ ] = {‘S’,’T’,’U’,’D’,’E’,’N’,’T’,’\0’};

1. Each character of the string occupies 1 byte of memory the last character is always’\0’.
2. It is not compulsory to write ‘\0’ in string. The compiler automatically puts ‘\0’ at the end
of the character array or string.
3. The characters of string are stored in contiguous (neighboring) memory locations.

Memory map of strings:-

5001 5002 5003 5004 5005 5006 5007 5008

S T U D E N T \0

DECLARING AND INITIALIZING STRINGS:-

General form:-

char string _name [size];

Where size determines the number of characters in the string _name.


char name [8] = “student”;
char name [8] = {‘S’,’T’,’U’,’D’,’E’,’N’,’T’};
char name [8] = {{‘S’},{‘T’},{‘U’},{‘D’},{‘E’},{‘N’},{‘T’}};
Ex:- #include<stdio.h>
void main ()
{
char name [7] = {‘S’,’T’,’U’,’D’,’E’,’N’,’T’};
2

printf(“name = %s”, name);


}
Output: student ___

garbage value
Ex:- :- #include<stdio.h>
void main ()
{
char name [7] = {‘S’,’T’,’U’,’D’,’E’,’N’,’T’};
char name1 [3] = {‘h’,’i’};
printf(“name=%s”,name);
printf(“name1 = %s”,name1);
}

Output: studenthi
hi

Ex: #include<stdio.h>
main()
{
printf("\nMemory Requirement of XYZ is %dbytes\n",sizeof("XYZ"));
}
Output: Memory Requirement of XYZ is 4bytes

Ex: #include<stdio.h>
main()
{
char s[10]=”xyz”;
printf("\nThe first character of the string literal XYZ is %c\n", *s);
printf("\nThe second character of the string literal XYZ is %c\n", *(s+1));
}

O/P: The first character of the string literal XYZ is X


The second character of the string literal XYZ is Y

Reading strings from terminal:- using scanf function


3

The familiar input function scanf can be used with %s format specification to read in a string of
characters.

Ex:-

char name[10];

scanf(“%s”,name);

The problem with the scanf function is that it terminates its input on the first white space it
finds.

Ex:- Vignan university

Computer programming

Then only the string ‘computer’ will be read into the array address, since the blank space after
the word ‘computer’ will terminate the reading of string.

The scanf function automatically terminates the string that is read with a null character.
The ampersand (&) is not required before the variable name.

If we want to read the entire line computer programming use two character arrays.

Ex:- char name1[10], name2[10];


scanf(“%s %s”, name1, name2);

Read string with spaces by using "%[^\n]" format specifier

The format specifier "%[^\n]" tells to the compiler that read the characters until "\n" is not found.

#include <stdio.h>
int main()
{
char name[30];
printf("Enter name: ");
scanf("%[^\n]",name);
printf("Name is: %s\n",name);
return 0;
}

Output: Enter name: computer programming


4

Name is: computer programming


We can also specify the field width using the form %ws in the scanf statement for reading a
specified number of characters from the input string.
Ex:- scanf (“%ws”, name);

Two things may happen:-

1. The width w >= The number of characters typed in. The entire string will be stored in the
string variable.
2. w < the number of characters in the string. The excess characters will be truncated& left
unread.
char name [10];
scanf(“%5s”, name);

The input string RAM stored.


R A M \0 …………
0 1 2 3

KRISHNA
K R I S H \0 ? ? ? ?
0 1 2 3 4 5 6 7 8 9

writing strings to screen:- using printf function:-

The printf function with %s format to print strings to the screen. The format %s can be used
to display on array of characters that is terminated by the null character.
Ex:- printf(“%s”,name);
we can also specify the precision with which the array is displayed.

printf function precision using:

various formats are shown to print the string with width specified.
1. printf(“%s”,text); prabhakar
2. printf(“%.5s”,text); prabh
3. printf(“%.8s”,text); prabhaka
4. printf(“%.15s”,text); prabhakar
5. printf(“%-10.4s”,text); prab
6. printf(“%11s”,text); prabhakar
5

1. The entire string can be displayed with first stmt.


2. We can specify the precision with character string. The precision is provided after decimal
point. which specify the 5 characters to be displayed.
3. The 8 characters are displayed.
4. It displays the entire string.
5. It is with - sign, displays the string with left justified.
6. When the field length is less than the length of the string the entire string printed.
w<string=string.
When field length is greater than the length of the string, blank spaces are initially printed
followed by the string.
w>string=(w-string length blank spaces)string.
7. When number of characters to be displayed is specified as zero after decimal point nothing
will be displayed.

using getchar function:-

The getchar() function is a part of standared ‘C’ language i/o library .It returns a single
character from a standared input device. The function does not require any arguments through a
pair of empty parentheses.

syntax:
character_variable=getchar();
Ex:-
char c;
c=getchar();
printf(“%c”,c);

using gets function:-

Reading a string of text containing whitespaces is to use the library function gets available in
the <stdio.h> header file.
syntax:- gets(string_variable);
Ex:-
char line[80];
gets(line);
printf(“%s”,line);

using putchar function:


6

It transmits a single character to a standard output device. The character being transmitted
will normally be expressed as an argument to the function enclosed in parentheses following the
word putchar.

syntax:- putchar(character_variable);

Ex:-
char c;
c=getchar();
putchar(c);

using puts function:


printing string values is to use the function puts declared in the header file <stdio.h>. This
is a one parameter function &invoked as
syntax:-
puts(str);
Ex:
char name[10];
gets(name);
puts(name);

Character Arrays:

A string is a collection of characters. A string constant is a one dimensional array of characters


terminated by a null (‘\0’) character.

Ex:- char name[ ]={‘A’, ‘K’, ‘S’, ‘H’, ‘A’, ’y’, ‘\0’};

Where ‘\0’ is a null character and specifies end of the string.

Terminating the string with null ‘\0’ is important, because it is the only way the
functions that work with a string can know where the string ends.

A K S H A Y \0
Each character in the array occupies a memory of one byte (including ‘\0’)

We can also declare strings like

Ex:- char name[ ]=”AKSHAY”;

In these declarations ‘\0’ is not necessary C inserts ‘\0’ automatically at the end of the string.
7

Ex:- Write a c program to print a character array.

#include<stdio.h>

void main()

char name[20];

printf(“Enter the name”);

scanf(“%s”,name);

printf(“The name is %s”,name);

Output: Enter the name AKSHAYA

The name is AKSHAYA

String Handling Functions or String manipulations:-

Character strings are often used to build meaningful and readable programs. The common
operations performed on character strings include:

Reading & writing strings

Combining strings together

Copying one string to another

Comparing strings for equality

Every C compiler provides a large set of string handling function which are contained in header
file <string.h>

The C library supports a large number of string-handling functions that can be used to carry out
many of the string manipulations.
8

Functions
Description
strlen()
Determines length of a string
strcpy()
Copies a string from source to destination
strcat()
Appends source string to destination string

strcmp() Compares characters of two strings(function discriminates


b/w small & capital letters)
strrev() Reverses all characters of a string

1.strlen function:

This function counts and returns the number of characters in a string.

Syntax: n= strlen(string);

Where n is a an integer variable, which receives the value of the length of the string. The
argument may be a string constant. The counting ends at the first null character.

Ex: 1. strlen(“rama rao”) = 8

2.strlen(“7893”)= 4

3.strlen(“+-$”) = 3

4.strlen(“A2$”) = 3

5.strlen(“ ”)= 0 // null string

6.char a[ ]=”swathi”;

Strlen(a)=6

7. strlen(7892) // error

/* C program to count the number of characters in a given string */

#include<stdio.h>

#include<string.h>

void main()
9

char text[20];

int len;

printf(“enter the text”);

gets(text);

len=strlen(text);

printf(“length of the string = %d”,len);

2.strcpy function:

This function copies the contents of one string to another.

Syntax: strcpy(string1,string2);

Assigns the contents of string2 to string1.

i.e. string2 is a source string

string1 is a destination string.

String2 may be a character array variable or a string constant.

1. strcpy(city,”delhi”);

Will assign the string “delhi” to the string variable city.

2. char a[20];

Strcpy(a,”rama rao”);

Rama rao is copied to array ‘a’. Array ‘a’ contains rama rao.

a=”rama rao” is invalid. We can’t use array name on left hand side of “=” as it modifies base
address of the array which is not possible.

3. char a[ ]=”7892”;

char b[10];
10

strcpy(b,a);

String in array a is copied to b. Array b contains “7892”

char a[ ]=”hyd”; is valid

char a=”sec”; is invalid.

We can use array name on left hand side of = , only in declaration statement but not in
executable statement.

4. char a[20], b[20];

strcpy(a,strcpy(b,”hyd”));

“hyd” is stored in arrays b and a. output of inner function is “hyd”.

/* C program to copy the contents of one string to another string by using strcpy()
*/

#include<stdio.h>

#include<string.h>

void main()

char ori[20], dup[20];

printf(“enter ur name”);

gets(ori);

strcpy(dup,ori);

printf(“original string : %s”,ori);

printf(“duplicate string: %s”,dup);

3.strcmp function:
11

This function compares two strings for finding whether they are same or different. This function
discriminates between small and capital letters.

Ex: char str1[20]=”HELLO”;

char str2[20]=”hello”;

these two strings are different.

Characters of these strings are compared one by one. In case of a mismatch the function returns
non-zero value otherwise zero i.e. when the two strings are same strcmp() returns the value zero.
If they are different it returns the numeric difference b/w the ASCII values of non-matching
characters.

Syntax: strcmp(string1,string2);

Ex: strcmp(“their”,”there”);

Will return a value of -9 which is the numeric difference between ASCII “i”(105) and ASCII
“r”(114) i.e. “i” minus “r” in ASCII code (105-114 ) is -9.

If the value is negative, string1 is alphabetically above string2.

/* C program to compare the 2 strings using strcmp() function */

#include<stdio.h>

#include<string.h>

void main()

char str1[10], str2[10];

int diff;

printf(“enter the string1”);

gets(str1);

printf(“enter the string2”);

gets(str2);

diff=strcmp(str1,str2);
12

if(diff==0)

puts(“the two strings are identical”);

else

puts(“the two strings are different”);

4.Strcat Function:

This function appends the target string to the source string.

Syntax: strcat(source,target);

Ex: char text1[30]=”I AM”;

char text2[30]=”AN INDIAN”;

strcat(text1, text2);

O/P: I AM AN INDIAN text1

/* C program to append second string at the end of the first string using strcat() */

#include<stdio.h>

#include<string.h>

void main()

char text1[30], text2[15];

printf(“enter text1 :”);

gets(text1);

printf(“enter text2 :”);

gets(text2);

strcat(text1, “ “);

strcat(text1, text2);
13

printf(“%s \n”, text1);

5 . Strrev Function:-

This function simply reverses the given string. This function takes only one argument and
returns one argument.

Syntax: strrev(string);

Ex: 1) char x[] = “Rama Rao”;

strrev(x);

Output: String in array x is reversed. Array x contains: oaR amaR

2) char x[]=”4000”;

strrev(x);

Output: String in array x is reversed. Array x Contains: 0004.

/* C Program to display reverse of the given string */

#include<stdio.h>

#include<string.h>

void main()

char text[20];

puts(“Enter the String: ”);

gets(text);

puts(“Reverse String is :”)

puts(strrev(text));

}
14

Functions
Description
strncpy()
Copies characters of a string to another string up to the
specified length
stricmp()
Compares two strings(function does not discriminate b/w
small & capital letters)

strncmp() Compares characters of two strings up to the specified length

strnicmp()
Compares characters of two strings up to the specified
length. Ignores case.
strlwr()
Converts upper case characters of a string to lower case
strupr()
Converts lower case characters of a string to upper case
strdup()
Duplicates a string
strchr()
Determines first occurrence of a given character in a string

strrchr()
Determines last occurrence of a given character in a string
strncat()
Appends source string to destination string up to a specified
length

1 .strncpy function:

This function copies specified length of characters from source to destination string.

Syntax: strncpy(destination,source,n);

Where n is the number of left most characters of the source string to be copied.

Ex: char s1[20]=”london”;

char s2[20];

strncpy(s2,s1,5);
15

This statement copies the first 5 characters of the source string s1 into the target string
s2.Therefore s2 contains “Londo”.

/* C program to copy source string to destination string up to a specified length. Length is


to be entered through the key board */

#include<stdio.h>

#include<string.h>

void main()

char str1[15], str2[15];

int n;

printf(“enter source string”);

gets(str1);

printf(“enter destination string”);

gets(str2);

printf(“enter no.of characters to replace in destination string”);

scanf(“%d”, &n);

strncpy(str2,str1,n);

printf(“source string : %s”, str1);

printf(“destination string : %s”,str2);

2 . Stricmp Function:

This function compares the two strings. The characters of the strings may be in lower case or
upper case, the function does not discriminate between them .i.e. this function compares two
strings without the case. If the strings are same it returns the zero otherwise the non-zero value.

Ex: char str1[20]=”HELLO”;

Char str2[20]=”hello”;
16

These two strings are same.

Syntax: stricmp(string1,string2);

/* C program to compare two strings using stricmp() function */

#include<stdio.h>

#include<string.h>

void main()

char str1[10], str2[10];

int diff;

printf(“enter the string1”);

gets(str1);

printf(“enter the string2”);

gets(str2);

diff=stricmp(str1,str2);

if(diff==0)

puts(“the two strings are identical”);

else

puts(“the two strings are different”);

3. Strncmp Function:

Comparison of two strings can be made up to certain specified length.

Syntax: strncmp (string1, string2, n);

This compares the left-most n characters of string1 to string2 and returns,

a) 0 if they are equal


b) –ve number if string1 sub string is less than string2
c) +ve number, otherwise
17

Ex: char str1 [20] =”HELLO”;

char str2 [20] =”HE MAN”;

strncmp (str1, str2, 2);

Returns zero. I.e. the strings are identical up to 2 characters

Strncmp () function discriminates between lowercase and uppercase letters.

Ex: char str1[20]=”HELLO”;

char str2[20]=”he man”;

strncmp(str1, str2, 2);

Returns non zero value i.e. the strings are different up to 2 characters.

/* C program to compare two strings up to specified length


*/

#include<stdio.h>

#include<string.h>

void main()

char str1[10], str2[10];

int n, diff;

printf(“enter the string1”);

gets(str1);

printf(“enter the string2”);

gets(str2);

printf(“enter the length up to which comparison is to be made:”);

scanf(“%d”,&n);

diff=strncmp(str1, str2, n);

if(diff==0)
18

puts(“the two strings are identical up to %d characters”, n);

else

puts(“the two strings are different”);

4 .Strnicmp Function:

This function compares the characters of the two strings to a specified length. This function does
not discriminate between lowercase and uppercase letters.

Syntax: strnicmp (string1, string2, n);

Ex: char str1[20]=”HELLO”;

char str2[20]=”he man”;

strnicmp(str1, str2, 2);

returns zero. I.e. the two strings are identical up to 2 characters .

/* C program to compare 2 strings up to specified length using strnicmp () */

#include<stdio.h>

#include<string.h>

void main()

char str1[10], str2[10];

int n, diff;

printf(“enter the string1”);

gets(str1);

printf(“enter the string2”);

gets(str2);
19

printf(“enter the length up to which comparison is to be made:”);

scanf(“%d”,&n);

diff = strnicmp(str1, str2, n);

if(diff==0)

puts(“the two strings are identical up to %d characters”,n);

else

puts(“the two strings are different”);

5 .Strlwr Function:

This function can be used to convert any string to a lower case.

Syntax: strlwr(string);

Ex: char s[10]=”HAI”;

strlwr(s);

It returns hai.

/* C program to convert upper case string to lower case using strlwr() */

#include<stdio.h>

#include<string.h>

void main()

char upper[15];

printf(“enter the string in upper case:”);

gets(upper);

printf(“\n After strlwr() : %s”,strlwr(upper));

}
20

6 .Strupr Function:

This function converts lower case string to upper case.

Syntax: strupr(string);

Ex: char s[10]=”good”;

strupr(s);

It returns GOOD.

/* C program to convert lower case string to upper case string */

#include<stdio.h>

#include<string.h>

void main()

char lower[15];

printf(“enter the string in lower case:”);

gets(lower);

printf(“\n After strupr() : %s”,strupr(lower));

7 . Strdup() Function :-

This function is used for duplicating a given string at the allocated memory which is
pointed by a pointer variable.

Syntax: text2=strdup(text1);

Where text1 is a string & text2 is a pointer.

Ex char text1[20]=” HAPPY”, *text2;

text2=strdup(text1);

Output: text2 contains: HAPPY

/* C Program to enter the string & get it’s Duplicate */


21

#include<stdio.h>

#include<string.h>

void main()

char text1[20], *text2;

printf(“ Enter Text1:”);

gets(text1);

text2=strdup(text1);

printf(“ Original String: %s \n”, text1);

printf(“ Duplicate String: %s\n”,text2);

8 . Strchr() Function :-

This Function returns the pointer to position in the first Occurrence of the Character
in the given string.

Syntax: chp=strchr(string,ch);

Where string is character array, ch is character variable and chp is a pointer which collects
address returned by strchr( ) function.

Ex: char string[30]= “HELLO”;

char ch=’L’, *chp;

chp=strchr(string,ch);

Output: character ‘L’ found in the given string.

/* C Program to find first Occurrence of a given Character of a given character in a given


string */

#include<stdio.h>

#include<string.h>
22

void main()

char string[30], ch,*chp;

printf(“ Enter the String :”);

gets(string);

printf(“ Enter character to find:”);

ch=getchar();

chp=strchr(string,ch);

if(chp)

printf(“ Character %c found in string :”, ch);

else

printf(“Character %c found in string:”,ch);

9 . Strrchr() Function :-

This function returns the pointer to a position in the first occurrence of the
character in the given string from ending.

Syntax: chp=strrchr(string,ch);

10 .Strncat Function:

This function concatenates a string with another up to the specified length.

Syntax: strncat(text1, text2, n);

This will concatenate the left most n characters of text2 to the end of text1.

Ex: char s1[30]=”BALA”;

char s2[15]=”GURUSWAMY”;

n=4
23

O/P: s1 contains BALAGURU

/* C program to append second string with specified (n) number of characters at the end of
the first string using strncat() */

#include<stdio.h>

#include<string.h>

void main()

char text1[30], text2[10];

int n;

printf(“enter text1 :”);

gets(text1);

printf(“enter text2 :”);

gets(text2);

printf(“enter number of characters to add:”);

scanf(“%d”,&n);

strncat(text1, “ “);

strncat(text1, text2, n);

printf(“%s \n”,text1);

}
Functions Description

strstr() Determines first occurrence of a given string in


another string

strset() Sets all characters of a string with a given symbol or


Argument

strnset() Compares characters of two strings up to the specified


length

strspn() Compares characters of two strings up to the specified


length. Ignores case.

strpbrk() Converts upper case characters of a string to lower


case
24

strstr() Function :-

This function finds second string in the first string. It returns the pointer location
from where the second string starts in the first string. In case the first occurrence in the string is
not observed, the function returns a NULL character.

Syntax: strstr(string1,string2);

Ex: char string1[30] =”HAVE A NICE DAY”;

char string2[30]=”DAY”;

char *chp;

chp=strstr(string1,string2);

Output: DAY string is present in first string.

/* C program for occurrence of second string in the first string */

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

char line1[30], line2[30],*chp;

clrscr();

printf(“enter line1 :”);

gets(line1);
25

printf(“enter line2 :”);

gets(line2);

chp=strstr(line1, line2);

if(chp)

printf(“%s string is present in given string”, line2);

else

printf(“%s string is not present in given string”,line2);

Strset Function :-

This function replaces every character of a string with the symbol given by the programmer.
i.e.; the elements of the string are replaced with the arguments given by the programmer.

Syntax: strset(string,symbol);

Ex: char string[15]=”LEARN C”;

strset(string,’y’);

Output: String Contains : yyyyyyy.

/* C Program to replace (set) Given string with given symbol. */

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

char string[15];

char symbol;

clrscr();
26

puts(“ Enter String :”);

gets(string);

puts(“ Enter symbol for Replacement :”);

scanf(“%c”, &symbol);

printf(“ Before strset() : %s\n”, string);

strset(string,symbol);

printf(“ After strset() : %s\n”,string);

Strnset Function :- This function is the same as that of strset(). Here the specified length is
provided.

Syntax: strnset(string,symbol,n);

Where ’ n’ is the number of characters to be replaced.

Ex: char string[15]= ”ABCDEFGHIJ” ;

char symbol= ‘+’;

int n=4;

strnset(string,symbol,n);

Output: ++++EFGHIJ

/* C Program to replace (set) given string with given symbol gor given number of
arguments */

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

{
27

char string[15];

char symbol;

int n;

clrscr();

puts(“ Enter String :”);

gets(string);

puts(“ Enter symbol for replacement :”);

scanf(“%c”, &symbol);

puts(How many string characters to be replaced :”);

scanf(“%d”,&n);

printf(“ Before strnset() : %s\n”, string);

strnset(string,symbol,n);

printf(“ After strnset(): %s\n”, string);

Strspn Function:- This function returns the position of the string from where the source array
does not match with the target one.

Syntax: strspn(string1,string2);

Ex: 1) char str1[15] =”GOOD MORNING”;

char str2[15]=”GOOD BYE”;

strspn(str1,str2);

Returns the value 5. i.e., After 5 characters there is no match.

2) char str1[15]=”BOMBAY”;

char str2[15]=”TROMBAY”;

strspn(str1,str2);
28

Returns the value Zero. i.e., After 0 characters there is no match.

/* C Program to enter 2 strings. Indicate after what character the lengths of the 2 strings
have no match */

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

char str1[20], str2[20];

int length;

clrscr();

puts(“ Enter String1:”);

gets(str1);

puts(“Enter String2:”);

gets(str2);

length=strspn(str1,str2);

printf(“ After %d characters there is no match:\n”,length);

strpbrk Function :-

This function searches the first occurrence of the character in a given string and
then it displays the string starting from that character.

Syntax: strpbrk(text1,text2);

Ex: char text1[20] =” INDIA IS GREAT “;

char text2[2] =”G”;

char *ptr;
29

ptr=strpbrk(text1,text2);

This function finds first occurrence of second string in the first string and returns that address
which is assigned to character pointer *ptr. The pointer ptr displays the rest string.

Output: GREAT

/*C Program to print given string from first occurrence of given character */

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

char *ptr;

char text1[20],text2[20];

clrscr();

printf(“ Enter String1:”);

gets(text1);

printf(“ Enter String2 :”);

gets(text2);

ptr=strpbrk(text1,text2);

puts(“string from given character :”);

printf(ptr);

Write a C program to copy one string into another without using string handling functions
#include<stdio.h>
#include<string.h>
main()
{
char s1[100], s2[100];
30

int i;
printf(“\n Enter the string : ”);
gets(s1);
i =0;
while(s1[i] ! = ‘\0’)
{
s2[i] = s1[i];
i++;
}
s2[i] = ‘\0’;
printf(“\n Copied string is : %s”, s2);
}
 Write a C program to append one string to another without using string handling
functions

#include<stdio.h>
#include<string.h>
 void concat(char[], char[]);
 int main() {
char s1[50], s2[30];
   printf("\nEnter String 1 :");
   gets(s1);
   printf("\nEnter String 2 :");
   gets(s2);
   concat(s1, s2);
   printf("nConcated string is :%s", s1);
 
   return (0);
}
void concat(char s1[], char s2[])
{
   int i, j;
 
   i = strlen(s1);
 
   for (j = 0; s2[j] != '\0'; i++, j++) {
      s1[i] = s2[j];
   }
 
   s1[i] = '\0';
}
31

Write a C program to reverse the string without using string handling functions

#include<string.h>
main()
{
char str[100], temp;
inti=0, j = 0;
printf(“\n Enter the string : ”);
gets(str);
j = strlen(str)-1;
while(i< j)
{
temp = str[j];
str[j] = str[i];
str[i] = temp;
i++;
j --;
}
printf(“\n The reversed string is”);
puts(str);
}

Write a C program to check whether two strings are identical or not (if not check which
string is higher) without using string handling functions

#include<stdio.h>
int main()
{
   char str1[30], str2[30];
   int i;
  printf("\nEnter two strings :");
   gets(str1);
   gets(str2);
 
   i = 0;
   while (str1[i] == str2[i] && str1[i] != '\0')
      i++;
   if (str1[i] > str2[i])
      printf("str1 > str2");
   else if (str1[i] < str2[i])
32

      printf("str1 < str2");


   else
      printf("str1 = str2");
 
   return (0);
}

Command Line Arguments:


33

The function main() in C can also accept arguments or parameters like the other
functions. It has two arguments argc(for argument count) and argv(for argument vector).It may
have one of the following two forms.

main(int argc,char *argv[])

(or)

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

Here argc represents the number of arguments and argv is a pointer to an array of Strings
or Pointer to Pointer to Character. The argument argv is used to Pass Strings to Character.The
arguments argc and argv are called as Program Parameters. After Successful complication, The
Program is executed by using an execution command. The execution command depends on the
System running the program .It is possible to pass the values of program parameters argv through
an execution command only. Since the values are available and obtained from the command line,
they are also known as command line arguments.

cline.c

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

if(argc<2)

printf(“invalid options”);

getch();

exit(0);

else

printf(“The name is =%s”,argv[1]); }

You might also like