Strings in C Programming , Notes On C string Functions with important questions (NEB Exams)

The string can be defined as one dimenensional array of characters terminated by null [‘\0’]. The characters arry or string is useed to manipulate text sucha as word or sentences. Eacha character in the array occupies one byte of memory and last character must alwaye be 0. The termination character (‘\0’) is important in a string since it is the only way to identify where the string ends. When we define a string as chara S[10], the character s[10] is implicitly initiated with null in memory.
The strings are manipulated by specific string functions. These are inbuilt functions and defined within string.h header file.
Declaration:
There are two ways to declare a string in C language
1) By Char Array
2) By string Literal

By Char Array:
char ch[6]={‘N’,’e’,’p’,’a’,’l’,’\0′}
We know that array index starts from 0, so it will be represented as:
0 1 2 3 4 5
|N|e|p|a|l|\0|
While declaring string, size is not mandatory. So we can write the above as code

String Literal:
char ch[] ={“Nepal”};
char ch[]={‘N’,’e’,’p’,’a’,’l’};

Transversing String.
Transvering string is sometime different from transversing an integer warry. We need to know the lenght of array to transverse an integer array whereas we may use the null character in case of sting to identnfy the end of the string and terminate the loop
There are two ways to transverse a string.
1) By using the lenght of string
2) Using the null character.

C gets() function
The gets() function enables the user to enter some character followed by the enter key. All the characters entered by user get stored in characters array. The gets() allow the user to enter the space separate strings.

Ptrogram to read String using gets() function
#include<stdio.h>
#include<conio.h>
void main()
{
char name[12];
printf(“Enter the string: “);
gets(name);
printf(“you have entered %s”,name);
getch();
}

C puts function:
Puts() function is very much similar to printf function. The put(s) function is used to print the string on the console which is previously read by using gets() function, scanf() function or initialized strings. puts() function prints an additional newline charcter with string which moves the cursor to the new line on console.

#include<stdio.h>
#include<conio.h>
void main()
{
char country[12]=”Nepal”;
printf(“Your Country is:”);
puts(country);
getch();
}

String Manipulation function:
C program has many string manupation functions. which are as follows

SNFunctionsDescription
1Strlen(string_variable_name)Returns the Length of string name
2strcat(first_string,second_string)Concats or join first string with second string. The result of the string is stored in first string
3strcmp(string1, string2)Compares the first string with second string. If both strings are same it returns 0
4strcpy(destination,source)Copies the contents of source string to destination string
5strrev(string_variable_name)Returns reverse string
6strlwr(string)Returns string character in lowercase
7strupr(string)regurns string characters in uppercase

Q. 1) Show program to demonstrate the following string functions.
a) strlen()
b) strcat()
c) strcmp()
d) strcpy()
e) strrev()
f) strlwr()
g) strupr()

a) strlen()

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

void main()
{
char ch[50]={"Bhaktapur is a beautiful city"};
int n;

n=strlen(ch);
printf("Length of the string is: %d",n);

getch();
}

Output:

Length of the string is: 29

b) strcat()

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

void main()
{
char source[20]={'K','h','w','o','p','a'};
char destination[20]={'N','e','p','a','l'};

strcat(source,destination);

printf("The concatenated string is: %s",source);



getch();
}

Output: The concatenated string is: KhwopaNepal

c) strcmp()

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

void main()
{
char text1[12],text2[12];
int n;

printf("Enter Text1: ");
scanf("%s",&text1);
printf("Enter Text2: ");
scanf("%s",&text2);

n=strcmp(text1,text2);

if(n==0)
  printf("Both text are equal");
else if(n<1)
  printf("text1 comes first");
else
  printf("Text2 comes first");
getch();
}

e) strrev()

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

void main()
{
char text[12], rev[12];

printf("Enter Your Text Here ");
scanf("%s",&text);

strcpy(rev,text);
strrev(rev);

printf("Reversed word is as follow: %s",rev);
  
getch();
}

f) strlwr()

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

void main()
{
char text[12], lower[12];

printf("Enter Your Text Here ");
scanf("%s",&text);

strcpy(lower,text);
strlwr(lower);

printf("Lowercase word is as follow: %s",lower);
  
getch();
}

g) strupr()

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

void main()
{
char text[12], upper[12];

printf("Enter Your Text Here ");
scanf("%s",&text);

strcpy(upper,text);
strupr(upper);

printf("Uppercase word is as follow: %s",upper);
  
getch();
}

IMPORTANT QUESTIONS

1. Write a progam to enter 5 strings and sort in ascending order and display them.

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

void main()
{
char arr[12][12];
int i,j,v;
char temp[12];

printf("Enter five strings");
for(i=0;i<5;i++)
{	
    scanf("%s",&arr[i]);	
}

//Putting in ascending order;
for(i=0;i<5;i++)
{
  for(j=i;j<5;j++)
  {
      if(strcmp(arr[i],arr[j])>0)
      {
      	strcpy(temp,arr[i]);
      	strcpy(arr[i],arr[j]);
      	strcpy(arr[j],temp);
      }
  }	
}

for(i=0;i<5;i++)
{
      printf("%s \n",arr[i]);
}

getch();
}

2. Write a program to count total number of vowels and consonants in a string.

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

void main()
{
char arr[12];
int i,n,vowel=0,consonant=0;
char temp[12];

printf("Enter strings: ");
scanf("%s",&arr);	

n=strlen(arr);


for(i=0;i<n;i++)
{
  if(arr[i]=='a' || arr[i]=='e' || arr[i]=='i' || arr[i]=='o' || arr[i]=='u' || arr[i]=='A' || arr[i]=='E' || arr[i]=='I' || arr[i]=='O' || arr[i]=='U')
  {
    vowel++;
  }
  else
  {
    consonant++;
  }
}
printf("Number of Vowels: %d \n Number of Consonent: %d",vowel,consonant);

getch();
}

Other Questions:

1. Describe any five string functions with example.