Thursday, July 14, 2011

Array And Strings :STRING (CHARACTER ARRAY)

STRING (CHARACTER ARRAY)

Character Arrays [String]

Strings are stored in C as character arrays terminated by the null character, '\0'.
Declaration of string if done as follows
char str[25];
char str1[30] = "MY STUDYROOM";
M
Y

S
T
U
D
Y
R
O
O
M
\0
............

0
1
2
3
4
5
6
7
8
9
10
11
12
............
29
Assigning a character literal to an array is done as follows.
char str1[] = "MY STUDYROOM";
char str2[] = "Nepal"
The compiler automatically sizes the arrays correctly. For this example, str1 is of length 16, str2 is of length 5.
Example 1: Program to find out the length of string
#include<stdio.h>
#include<conio.h>
main()
{
int len;
char str[25];

printf("Enter any string");
scanf("%s", str);

/*loop has semicolon(;) at the end coz loop doesn't contain any statement below it*/
for(len=0; str[len]!='\0' ; len++);

printf("length of string is %d", len);

getch();
}
Example: Write a program to reverse the string
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
    int i, j, len;
    char str[25], rstr[25];
   
    printf("Enter any string");
    scanf("%s", str);
 
    //finding out the length of string
    len = strlen(str);
 
//reversing the string
for(i=0, j=len-1; j>=0; j--, i++)
          rstr[i] = rstr[j];
 
printf("Reverse sting is %s", rstr);
 
getch();
}
Example : Write a program to find out no. of vowel, consonant, digit, white space and other character in a sentence provided by an user.
#include<stdio.h>
#include<conio.h>
main()
{
    int i, v=0, c=0, d=0, ws=0, oth=0;
    char str[25];
   
    printf("Enter any string");
    scanf("%s", str);
 
    for(i=0; str[i]!='\0' ; i++)
{
if(str[i]=='a'||str[i]=='e'||str[i]=='i'||str[i]=='o'||str[i]=='u')
                v = v + 1;
else
                if(str[i]>='a' && str[i]<='z')
                      c = c + 1;
                else
                      if(str[i]>='0' && str[i]<='9')
                            d = d + 1;
                      else
                            if(str[i]==' ' )
                                  ws = ws + 1;
                            else
                                  oth = oth + 1;
}
  printf(" No. of vowel=%d, Consent=%d, Digit=%d, White space=%d and Other=%d", v, c, d, ws, oth);
  getch();
}

Library Functions for String Manipulation

To use any of these functions in your code, the header file "strings.h" must be included.
strlen():
strlen finds the length of string.
Syntax:
int strlen(char* )
Example:
char str[]="MY STYDYROOM";
int len;
len=strlen(str);
output:
len=12

strcpy():
strcpy copies a string, including the null character terminator from the source string to the destination.
Syntax:
Char* strcpy(char* source_string, char* destination_string)
Example:
char dst[25], src[]="MY STYDYROOM";
strcpy(dst, src);
output:
dst = "MY STYDYROOM"
strcat():
This function appends a source string to the end of a destination string.
Example:
char dst[25]="MY ";
char src[ ] = "STUDYROOM";
strcat(dst, src);
Output:
dst = "MY STYDYROOM"
strcmp
This function compares two strings.
  • If the first string is greater than the second, it returns a number greater than zero.
  • If the second string is greater, it returns a number less than zero.
  • If the strings are equal, it returns 0.
int x;
char str1[25], str2[25];
x = strcmp(str1, str2);
o   x > 0 - if the str1 is greater than str2. [i.e. str1="nepal" and str2="japan" ]
o   x< 0  - if the str1 is less than str2. [i.e. str1="japan" and str2="nepal"]
o   x==0 - if str1 is equal to str2. [i.e. str1="nepal" and str2="nepal" ]

Example: Write a program to find out the string is palindrome or nor.
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
    char str[25], str1[25];
   
    printf("Enter any string");
    scanf("%s", str);
   
    str1 = strrev(str);
 
    if(str==str1)
          printf("Entered string is PALAINDROME");
    else
          printf("Entered string is NOT PALAINDROME");
   getch();
}

0 comments:

Post a Comment