Thursday, December 26, 2013

Assignment 6: Strings SOLUTIONS

Strings
[SET – 1]

1
Write a program to find the length of string. 
#include<iostream.h>
#include<conio.h>
#include<stdio.h>

int main( )
{
 clrscr( );
 char str[80];
 cout<<"Enter a string:";
 gets(str);

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

 cout<<"Lenght of string is :"<<i;

 getch();
 return 0;
}

2
Write a program to display string from backward.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>

int main( )
{
 clrscr( );
 char str[80];
 cout<<"Enter a string:";
 gets(str);

 for(int l=0; str[l]!='\0';l++);  //Loop to find the length of the string

 for(int i=l-1;i>=0;i--)   //Loop to display the string backwards
  cout<<str[i];

 getch();
 return 0;
}


3
Write a program to count number of words in string.


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

int main( )
{
 clrscr( );
 char str[80];
 int words=0;
 cout<<"Enter a string:";
 gets(str);

 for(int i=0;str[i]!='\0';i++)
 {
  if (str[i]==' ')
   words++;   //Checking for spaces
 }

 cout<<"The number of words="<<words+1<<endl;

 getch();
 return 0;
}


4
Write a program to concatenate one string contents to another. 
#include<iostream.h>
#include<conio.h>
#include<stdio.h>

int main( )
{
 clrscr( );

 char str1[80],str2[80];
 cout<<"Enter first string :";
 gets(str1);
 cout<<"Enter second string :";
 gets(str2);

 for(int l=0;str1[l]!='\0';l++);

 for(int i=0;str2[i]!='\0';i++)
  str1[l++]=str2[i];

 str1[l]='\0';

 cout<<"\nThe first string after adding second string content is\n\n"<<str1;

 getch();
 return 0;
}

5
Write a program to compare two strings they are exact equal or not. 
#include<iostream.h>
#include<conio.h>
#include<stdio.h>

int main( )
{
 clrscr( );
 char STR1[80],STR2[80];

 cout<<"Enter first string:";
 gets(STR1);

 cout<<"Enter second string:";
 gets(STR2);

 for (int I=0; STR1[I]==STR2[I] && STR1[I]!= '\0' && STR2[I] != '\0'; I++);

 if(STR1[I]-STR2[I]==0)
  cout<<"Strings are equal";
 else
  cout<<"Strings are not equal";

 getch();
 return 0;
}


6
Write a program to check a string is palindrome or not. 
#include<iostream.h>
#include<conio.h>
#include<stdio.h>

int main( )
{
 clrscr( );
 char str[80];
 cout<<"Enter a string:";
 gets(str);

 for(int L=0;str[L]!='\0';L++);  //To find length of the string

 for(int C=0;(C<L/2) && (str[C]==str[L-C-1]);C++);

 if(C==L/2)
  cout<<"Palindrome";
 else
  cout<<"Not a palindrome";

 getch();
 return 0;
}
7
Write a program to find a substring within a string. If found display its starting position. 
#include<iostream.h>
#include<conio.h>
#include<stdio.h>

int main( )
{
 clrscr( );

 char str1[80],str2[80];
 cout<<"Enter first string :";
 gets(str1);
 cout<<"Enter second string :";
 gets(str2);

 for(int l=0;str2[l]!='\0';l++);    //finding length of string 2

 for(int i=0,j=0;str1[i]!='\0'&& str2[j]!='\0';i++)
  if(str1[i]==str2[j])
   j++;
  else
   j=0;

 if(j==l)
  cout<<"Substring found at position "<<i-j+1;
 else
  cout<<"Substring not found";
 
 getch();
 return 0;
}


8
Write a program to reverse a string. 
#include<iostream.h>
#include<conio.h>
#include<stdio.h>

int main( )
{
 clrscr( );
 char str[80];
 int temp;
 cout<<"Enter string :";
 gets(str);
 
 for(int l=0;str[l]!='\0';l++);    //finding length of string

 for(int i=0,j=l-1;i<l/2;i++,j--)
 {
  temp=str[i];
  str[i]=str[j];
  str[j]=temp;  
 }

 cout<<"Reverse String is\n"<<str;
 
 getch();
 return 0;
}
9
Write a program to convert a string in lowercase. 
#include<iostream.h>
#include<conio.h>
#include<stdio.h>

int main( )
{
 clrscr( );
 char str[80];
 cout<<"Enter a string:";
 gets(str);

 for(int i=0;str[i]!='\0';i++)
  str[i] = (str[i]>='A' && str[i]<='Z')?(str[i]+32):str[i];

 cout<<str;

 getch();
 return 0;
}


10
Write a program to convert a string in uppercase. 
#include<iostream.h>
#include<conio.h>
#include<stdio.h>

int main( )
{
 clrscr( );
 char str[80];
 cout<<"Enter a string:";
 gets(str);

 for(int i=0;str[i]!='\0';i++)
  str[i] = (str[i]>='a' && str[i]<='z')?(str[i]-32):str[i];

 cout<<str;

 getch();
 return 0;
}



No comments:

Post a Comment