Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
110 views

Array and Strings

Strings in C are defined as arrays of characters that are terminated with a null character '\0'. Strings can be declared and initialized like character arrays. Common string functions in the standard string.h library include strlen to return the length of a string, strcpy to copy one string to another, and strcat to concatenate two strings. Sample programs are provided to demonstrate declaring, initializing, reading, and manipulating strings in C.

Uploaded by

tovilas
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
110 views

Array and Strings

Strings in C are defined as arrays of characters that are terminated with a null character '\0'. Strings can be declared and initialized like character arrays. Common string functions in the standard string.h library include strlen to return the length of a string, strcpy to copy one string to another, and strcat to concatenate two strings. Sample programs are provided to demonstrate declaring, initializing, reading, and manipulating strings in C.

Uploaded by

tovilas
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Topic: Arrays and strings.

Strings in C
Strings are defined as an array of characters. The difference between a character array and a string
is the string is terminated with a special character ‘\0’.
Declaration of strings: Declaring a string is as simple as declaring a one dimensional array.
Syntax:
char str_name[size];
In the above syntax str_name is any name given to the string variable and size is used define the
length of the string, i.e the number of characters strings will store. Please keep in mind that there
is an extra terminating character which is the Null character (‘\0’) used to indicate termination of
string which differs strings from normal character arrays.
Example:
char str[5];

Initializing a String:
Below is an example to declare a string with name as str and initialize it with “FYIT Class”
char str[] = " FYIT Class s";
OR
char str[50] = " FYIT Class ";
OR
char str[] = {'F','Y','I','T', ' ','C','l','a','s','s','\0'};
OR
char str[50] = {'F','Y','I','T', ' ','C','l','a','s','s','\0'};

Memory representation of above code:

str[0] str[1] str[2] str[3] str[4] str[5] str[6] str[7] str[8] str[9] str[10]

F Y I T C l a s s '\0'

Note:
String can also be initialized using pointers as:
char *str = "abcd";
Example 1:
// C program to illustrate strings

#include<stdio.h>
int main()
{
// declare and initialize string
char str[] = "Saurabh";

// print string
printf("%s",str);

return 0;
}

Output:
Saurabh

Example 2:
Below is a sample program to read a string from user:

#include<stdio.h>

int main()
{
// declaring string
char str[50];

printf("\n Enter your name: ");

// reading string
scanf("%s",str);

// print string
printf("You have entered: %s",str);

return 0;
}
Example 3:
Find the Frequency of Characters a number of times character present
#include <stdio.h>

int main()
{
char str[100], ch;
int i, frequency = 0;

printf("Enter a string: ");


gets(str); /* scanf("%s",str);

printf("Enter a character to find the frequency: ");


scanf("%c",&ch);

for(i = 0; str[i] != '\0'; ++i)


{
if(ch == str[i])
frequency++;
}

printf("Frequency of %c = %d", ch, frequency);

return 0;
}
Output

Enter a string: University of Mumbai


Enter a character to find the frequency: i
Frequency of i = 3

Example 4:
C program to read line of text using gets() and puts().

#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;
}

Output
Enter name: Saurabh
Name: Saurabh
String functions in C:
C supports a large number of string handling functions in the standard library"string.h".

No. Function Description


1) strlen(string) Returns the length of string name.
2) strcpy(string1, string) Copies the contents of string2 to string1.
It joins first string with second string. The result of
3) strcat(string1, string2)
the string is stored in first string.
Returns 0 if string1 is same as string2. Returns <0 if
4) strcmp(string1, string2)
stringl < string2. Returns >0 if string1 > string2
5) strrev(string) Returns reverse string.
6) strlwr(string) Returns string characters in lowercase.
7) strupr(string) Returns string characters in uppercas

#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;
}

Output:
strcpy( str3, str1) : Hello
strcat( str1, str2): HelloWorld
strlen(str1) : 10

You might also like