Showing posts with label Strings Non-Recursive C Program. Show all posts
Showing posts with label Strings Non-Recursive C Program. Show all posts

Wednesday, 29 October 2014

Palindrome ( with built-in functions )

Aim:
To check given string is palindrome or not.

Program:
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main ()
  {
     char str[80],revstr[80];
     clrscr ( );
printf("\nEnter a string:");
   scanf("%s",&str)
   strcpy(revstr,str);
   strrev(revstr);
if(!strcmp(str,revstr))
   printf("\nGiven string '%s' is Palindrome",str);
else
   printf("\nGiven string '%s' is not Palindrome",str);
}

Output:
Enter a string: medem
Given string 'medem' is Palindrome

Sub-sting in Main string

Aim:
To find sub-string in a given string.

Program:
#include<stdio.h>
#include<conio.h>
void main ( )
{
   int count=0,count2=0,i,j,flag;
   char str[80],search[20];
clrscr();
printf("\n Enter a string:");
gets(str);
printf("\n Enter a sub-string:");
gets(search);
  while( str[count] != '\0')
    count++;
  while( search[count2] != '\0')
    count2++;
for(i=0;i<=count1-count2;i++)
  {
    for(j=i;j<i+count2;j++)
      {
        flag =1;
        if( str[i] != search[j-i] )
          {
            flag = 0;
            break;
          }
      }
  if( flag == 1)
      break;
}
  if( flag == 1)
printf("\n SEARCH SUCCESSFUL");
  else
printf("\n SEARCH NOT SUCCESSFUL");
}

Palindrome ( without built-in functions )

Aim:
To check given string is Palindrome or not.

Program:
#include<stdio.h>
#include<conio.h>
void main ( )
  {
     char str[80],revstr[80];
     int i,length=0,flag=0;
     clrscr ();
printf("\n Enter a string:");
gets (str);
for( i=0;string[i] != '\0';i++)
   {
     length++;
   }
printf("\nLength of the string \'%s'\ is : %d",str,length);
for(i=length-1;i>=0;i--)
   {
     revstr[length-i-1] = str[i];
   }
for(i=0;i<length;i++)
  {
     if(revstr[i] == str[i])
           flag = 1;
     else
          flag = 0;
    }
   if ( flag == 1)
printf("\n%s is a palindrome",str);
   else
      printf("\n%s is not a palindrome",str);
}

Tuesday, 28 October 2014

String insertion at specified location

Aim:
To insert a substring into a string.

Program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
   char str[80],substr[20];
   char X,newstr[80];
   int pos,count1=0,count2=0,
   int i=0,t=0,l;
   clrscr ();
printf("Enter a string:");
gets(str);
printf("Enter a substring:");
gets(substr);
while( str[i] != '\0' )
    {
       count1++;
       i++;
    }
while( substr[i] != '\0' )
    {
       count2++;
       i++;
    }
printf("\nEnter the position to insert the substring");
scanf("%d",&pos);
strcpy(newstr,str);
l = pos+count2;
for( i=pos;i<=count1+count2;i++)
  {
    X = newstr[i];
    if(t < count2)
     {
       str[i] = substr[t];
          t = t+1;
      }
      str[l] = X;
      l++;
   }
printf("\nNewstring is: %s",str);
}

Program:
Enter a string: hydai
Enter a substring: un
Enter the position to insert the substring: 3
Newstring is hyundai

String Deletion from specified position

Aim:
To insert a substring into a string.

Program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
   char str[80],newstr[80];
   int pos,len,count1=0,
   int i=0,t=0 ;
   clrscr ();
printf("Enter a string:");
gets(str);
while( str[i] != '\0' )
    {
       count1++;
       i++;
    }
printf("\nEnter the position to delete the characters:");
scanf("%d",&pos);
printf("\nEnter the number characters to delete:");
scanf("%d",&len);
t = pos;
for( i=pos+len;i<=count1-len;i++)
  {
       str[t] = substr[i];
          t = t+1;
}
printf("\nNewstring is: %s",str);
}

Program:
Enter a string: hyabundai
Enter the position to delete the characters: 3
Enter the number of characters to delete : 2
Newstring is: hyundai

String leangth using without library functions

Aim: String length using while or for loop.

Program:
Code-1:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main ()
{
  char str[80];
  int length=0,i=0;
clrscr ();
printf("Enter a string:");
gets(str);
  while( str[i] != '\0' )
    {
      length++;
       i++;
    }
printf("\nLength of a string is : %d",length);
}

Code-2:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main ()
{
  char str[80];
  int length=0;
clrscr ();
printf("Enter a string:");
gets(str);
  for( i=0; str[i] != '\0'; i++ )
    {
      length++;
    }
printf("\nLength of a string is : %d",length);
}

To Reverse a string using library function

Aim:
To Reverse a string using library functions

Program:

Code-1:
#include< stdio.h>
#include<string.h>
void main ()
  {
    char str[80];
    int i;
printf("Enter a String:");
gets(str);
printf("\nReverse string is:");
   for(i=strlen(str)-1;i>0;i--)
      printf("%c",str[i]);
  }

Output:
Enter a string: hahaha
Revere string is: ahahah

Code-2:
#include< stdio.h>
#include<string.h>
void main ()
  {
    char str[80];
printf("Enter a String:");
gets(str);
  printf("%s\n",str);
printf("Reverse string is:%s",strrev(str));
  }

Output:
Enter a string: cprogram
Reverse string is: margorpc

Basic String Handling Functions

Aim:
To demonstrate the string Handling Functions.

Program:
#include<stdio.h>
#inude<string.h>
void main ()
{
   char S1[50],S2[50];

puts("Enter first string:");
gets(S1);
puts("\nEntet second string:");
gets(S2);

printf("\nleangths of S1 is %d and S2 is %d.",strlen (S1),strlen (S2));

if( !strcmp(S1,S2) )
     printf("\ntwo strings are equal.");
else
     printf("\ntwo strings are not equal.");

strcat(S1,S2);
      printf("\n%s",S1);

strcpy( S1,"This is a test");
       printf("%s\n",S1);

if( strchr("hello",'e');
        printf("e is in hello\n");

if( strstr("hi these","hi");
        printf("found hi");
}

Output:
Enter first string: hello
Enter second string: hello
leangths of S1 is 5 and S2 is 5.
two strings are equal
hellohello
This is a test
e is in hello
found hi

Monday, 27 October 2014

Compare Strings

Non-Recursive C Program:

Aim: To compare two strings

Program:
#include<stdio.h>
#include<conio.h>
void main()
{
  int count1=0,count2=0,flag=0,i; char str1[10],str2[10];
clrscr();
puts("Enter a string:");
gets(str1);
puts("Enter another string:"); gets(str2);

while (str1[count1]!='\0')
     count1++;
while (str2[count2]!='\0')
     count2++; i=0;

while ( (i < count1) && (i < count2))
{
    if (str1[i] == str2[i])
     {
        i++;
        continue;
      }
 
   if (str1[i]<str2[i])
      {
         flag = -1;
          break;
       }
        
  if (str1[i] > str2[i])
          {
             flag = 1;
             break;
          }
    }

if (flag==0)
     printf("Both strings are equal\n");
if (flag==1)
       printf("String1 is greater than string2\n", str1, str2);
if (flag == -1)
     printf("String1 is less than string2 \n", str1, str2);
  getch();
  }