-->

Facebook

Jasper Roberts - Blog

Saturday, April 4, 2015

Chapter 4 Array & String

Concepts of array, declaration of one dimensional array,  | CLICK HERE
two dimensional arrays,                                                      | CLICK HERE
String and Built-in-string functions.                                     | CLICK HERE







String and Built-In String Functions.


String

-         String is a sequence of characters enclosed in double quotes.

-         Normally, string is useful for storing data like name, address, city etc.

-         ASCII code is internally used to represent string in memory.

-         In C each string is terminated by a special character called null character.

-         In C language, null character is represented as ‘\0’ or NULL.

-         Because of this reason, the character array must be declared one size longer than the string required to be stored.

S
T
U
D
E
N
T
\0
  Because of this reason, the character array must be declared one size longer than the string required to be stored.

-         - Here, the string stored is “STUDENT”, which is having only 8 characters, but actually in memory 9 characters are stored because of NULL character at the end.

-      -  char name[50];
-         This line declares an array name of characters of size 50. We can store any string up to maximum length of 49.

-       -  String is basically an array of characters, so we initialize the string by using the method of initializing the single dimensional array as shown below:

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

-     -  When the string is written in double quotes, the NULL character is not required, it is automatically taken.

-         In-built String Functions:

String Functions
Syntax
Meaning
strlen()
strlen(s1)
It is used to find out the length of string s1.
strcpy()
strcpy(s1,s2)
It is used to copies the string s2 to s1.
strcat()
strcat(s1,s2)
It is used to concate string s2 at the end of string s1.
strcmp()
strcmp(s1,s2)
It is used to compares string s1 with s2. If both string are equal, it returns 0 otherwise returns negative number.
strrev()
strrev(s1)
It is used to reverse the string s1, the original string is overwritten.
strupr()
strupr(s1)
It is used to convert string s1 to uppercase.
strlwr()
strlwr(s1)
It is used to convert string s1 to lowercase.
strstr()
strstr(s1,s2)
It returns a pointer to the first occurrence of string s2 in s1.

Possible Questions:
1.     What is string? Explain in-built string functions.
2.     What is string? Explain the following string functions.
strlen()
strcpy()
strcat()
strcmp()
strrev()
3.     What is string? Explain built-in string functions with example.

Write a C Program to display the ASCII value of a given string.


Difference between:



Difference Between gets() and scanf()

gets()
scanf()
1.     It is used to read string.
1.     It is used to read integer, character, float and string also.
2.     It can read string having white space, tabs.
2. It cannot read string having white space.




Difference Between puts() and printf()
puts()  
printf()
1.     It is used to print string.
1.     It is used to print integer, float, character and string also.
%s specifier required to print string.
2.     It will automatically print new line after the string.
2.     It does not automatically print the new line character.
We can provide newline by using “\n”


Two Dimensional Arrays

Two Dimensional Arrays

-         Sometimes we need to store the data where more than one dimensions are involved, like sales information of a company, or for mathematical calculations we need to use matrix.

-         To declare a two-dimensional integer array of size i,j,  you would write something as follows:

-         Syntax:
Data-Type Variable-Name[row-size][col-size];

Where , Data-Type may be int, char or float,
Variable-Name represents name of the array,
Row-size indicate the number of row in table,
Col-size indicate the number of columns in an array.
-         Example:
                     int matrix[3][3];

-         In above example, int is a datatype and matrix is an array name. Here, the size of row is 3 and size of column is 3.






In above figure, the cell a[i][j] refers to the cell in ith row jth column.
So,
matrix[0][0]=10,
matrix[1][0]=20,
matrix[2][0]=30,
matrix[0][1]=40,
matrix[1][1]=50,
matrix[2][1]=60,
matrix[0][2]=90,
matrix[1][2]=80,
matrix[2][2]=70,
and like that.
So , solving mathematical problems involving matrices, we can use two dimensional array.



Write a C++ Program to demostrate character data type.

#include<iostream.h>
//using namespace std;
class Person
{
char name[30];
int age;
public:
void get_data(void);
void display();
};
void Person::get_data(void)
{
cout<<"Enter the name : ";
cin>>name;
cout<<"\nEnter the age : ";
cin>>age;
};
void Person::display(void)
{
cout<<"\nName : "<<name;
cout<<"\nAge : "<<age;
};
void main()
{
Person p;
p.get_data();
p.display();
}

Write a C++ Program to calculate average and sum.

#include<iostream.h>
//using namespace std;
void main()
{
float x,y,s,a;
cout<<"Enter the number : "<<"\n";
cin>>x>>y;
s=x+y;
a=s/2;
cout<<"\nSum = "<<s;
cout<<"\nAvg = "<<a;
}

OUTPUT:
Enter the number: 10
10
Sum = 20
Avg = 10

Basic Concepts of Array

Array:
One Dimensional Array - CLICK HERE

Write a C Program to Find out length of string using strlen( ) function.

PRACTICAL-SET-5
2.   Find length of string using strlen( function

#include<stdio.h>
#include<conio.h>
#include<string.h>

void main()
{
int i;
char str[30];

clrscr();

printf("Enter the string:");
scanf("%s",&str);

i = strlen(str);

printf("Length of the entered string: %d",i);

getch();
}

OUTPUT:
String Length
String Length



Write a C program to count total words in text.

PRACTICAL-SET-5
1.   Write a progratcount total words in text.

#include<stdio.h>
#include<conio.h>
#include<string.h>

void main()
{
    char s1[50];
    int i, count = 0;
    clrscr();

    printf("Enter the string:");
    scanf("%[^\n]s", s1);
    for (i = 0;s1[i] != '\0';i++)
    {
        if (s1[i] == ' ')
            count++;    
    }
    printf("Total words in entered string are: %d\n", count + 1);
getch();

}

OUTPUT:
c programs
string length