Wednesday, September 11, 2013

program of string...



String operations
·        Finding length of a string

C program to find string length
#include <stdio.h>
#include <string.h>

int main()
{
   char a[100];
   int length;
   printf("Enter a string to calculate it's length\n");
   gets(a);
    length = strlen(a);
    printf("Length of entered string is = %d\n",length);
    return 0;
}

C program to find string length without strlen
#include<stdio.h>
#include<conio.h>
void main()
{
    char a[50];
    int i,j;
    clrscr();
    printf("\n Please Give The STRING : ");
    scanf("%s",a);
    for(i=0,j=0;a[i]!='\0';i++)
        j++;
    printf("\n THE STRING LENGTH OF %s IS %d .",a,j);
    getch();
}

C program to find length of a string using pointers.
#include <stdio.h>

int main()
{
   char array[100], *pointer;
   int length = 0;
    printf("Enter a string\n");
   gets(array);
    pointer = array;

   while(*(pointer+length))
      length++;

   printf("Length of entered string = %d\n",length);

   return 0;
}
//Function to find string length
int string_length(char *s)
{
   int c = 0;

   while(*(s+c))
      c++;

   return c;
}

·        Converting Characters of a string into upper case and lower case

C program to uppercase the string using strupr().
#include<stdio.h>
//#include<conio.h>
void main()
{
char str[25];
int len;
//clrscr();
printf("Enter the String: ");
gets(str);
strupr(str);
printf("\nYour String in Uppercase %s",str);
}

C program to uppercase the string without using strupr()
#include<stdio.h>
#include<conio.h>
void main()
{
char str[25];
int len,i=0;
printf("Enter the String: ");
gets(str);
while(str[i]!='\0')
{
 if(str[i]>96&&str[i]<123)
 str[i]=str[i]-32;
 i++;
}
printf("\nYour String in Uppercase %s",str);
}

C program to uppercase the string without using strupr() using Pointers.
#include<stdio.h>
void upper(char *);
void main()
{
char str[25];
int len,i=0;
printf("Enter the String: ");
gets(str);
upper(str);
printf("\nYour String in Uppercase %s",str);
}
void upper(char *str1)
{
while(*str1!='\0')
{
 if(
*str1>96&&*str1<123)
 *str1=*str1-32;
str1++;
}
}

C program to lowercase the string using strlwr().
#include<stdio.h>
//#include<conio.h>
void main(){
char str[25];
int len;
//clrscr();
printf("Enter the String: ");
gets(str);
strlwr(str);
printf("\nYour String in Lowercase %s",str);
}

C program to lowercase the string without using strlwr().
#include<stdio.h>
#include<conio.h>
void main(){
char str[25];
int len,i=0;
clrscr();
printf("Enter the String: ");
gets(str);
while(str[i]!='\0'){
 if(str[i]<91&&str[i]>64)
 str[i]=str[i]+32;
 i++;
}
printf("\nYour String in Lowerercase %s",str);
}

C program to lowercase the string without using strlwr() using pointers
#include<stdio.h>
#include<conio.h>
void lower(char *);
void main()
{
char str[25];
int len;
clrscr();
printf("Enter the String: ");
gets(str);
lower(str);
printf("\nYour String in Lowercase %s",str);
}

void lower(char *str1)
{
while(*str1!='\0')
{
 if(*str1<91&&*str1>64)
 *str1=*str1+32;
 str1++;
}
}

·        Concatenation of two strings to form a new string

C program to concatenate two strings using strcat().
#include <stdio.h>
#include <string.h>
void main(void)
{
   char str1[25],str2[25],str3[50];
   printf("\nEnter First String:");
   gets(str1);
   printf("\nEnter Second String:");
   gets(str2);
   strcpy(str3,str1);                      
   strcat(str3,str2);                      
   printf("\nConcatenated String is %s",str3);
}

C program to concatenate two strings without using strcat()
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main(){
     char str1[20],str2[20],str3[40]="";
     int len,i,j;
     clrscr();
     printf("Enter string1: ");
     scanf("%s",str1);
     printf("Enter string2: ");
     scanf("%s",str2);
     for(i=0,j=0;str1[i]!='\0';i++,j++)
     str3[j] = str1[i];

     for(i=0;str2[i]!='\0';i++,j++)
     str3[j] = str2[i];

     str3[j] = '\0';
     len = j;
     printf("\n\nAfter Concatination : %s",str3);
     printf("\n\nLength of concated string is %d",len);
     getch();
}
C program to concatenate two strings without using strcat() and using pointers
#include<stdio.h>
void concat(char* ,char* );
void main(void)
{
   char str1[25],str2[25];
   printf("\nEnter First String:");
   gets(str1);
   printf("\nEnter Second String:");
   gets(str2);
   concat(str1,str2);                               
   printf("\nConcatenated String is %s",str1);
}
void concat(char *s1,char *s2)
{
     while(*s1!='\0')                               
             s1++
     while(*s2!='\0')                               
     {
            *s1=*s2;                               
             s1++;                                        
             s2++;                                        
     }
      *s1='\0';                                     
}

·        Reversing a string

C programming code
#include <stdio.h>
#include <string.h>

int main()
{
   char arr[100];
    printf("Enter a string to reverse\n");
   gets(arr);
    strrev(arr);
    printf("Reverse of entered string is \n%s\n",arr);
    return 0;
}
String reverse in c without using string function
#include<stdio.h>
int main(){
char str[50];
char rev[50];
int i=-1,j=0;

printf("Enter any string : ");
scanf("%s",str);
while(str[++i]!='\0');

while(i>=0)
rev[j++] = str[--i];

rev[j]='\0';
printf("Reverse of string is : %s",rev);
return 0;
}

C program to reverse a string using pointers
#include<stdio.h>
int main(){
char str[50];
char rev[50];
char *sptr = str;
char *rptr = rev;
int i=-1;

printf("Enter any string : ");
scanf("%s",str);
while(*sptr){
sptr++;
i++;
}

while(i>=0){
sptr--;
*rptr = *sptr;
rptr++;
--i;
}

*rptr='\0';
printf("Reverse of string is : %s",rev);
return 0;
}

·        Copying a string

C program to copy a string using strcpy
#include<stdio.h>
#include<string.h>

main()
{
   char source[] = "C program";
   char destination[50];

   strcpy(destination, source);

   printf("Source string: %s\n", source);
   printf("Destination string: %s\n", destination);

   return 0;
}

C program to copy a string without strcpy
#include <stdio.h>
int main()
{
char s1[100], s2[100], i;
printf("Enter string s1: ");
scanf("%s",s1);
for(i=0; s1[i]!='\0'; ++i)
{
s2[i]=s1[i];
}
s2[i]='\0';
printf("String s2: %s",s2);
return 0;
}

C program to copy a string using pointers
#include<stdio.h>

void copy_string(char*, char*);

main()
{
    char source[100], target[100];

    printf("Enter source string\n");
    gets(source);

    copy_string(target, source);

    printf("Target string is \"%s\"\n", target);

    return 0;
}

void copy_string(char *target, char *source)
{
   while(*source)
   {
      *target = *source;
      source++;
      target++;
   }
   *target = '\0';
}


·        Comparing strings

C program to compare two strings using strcmp
#include <stdio.h>
#include <string.h>

int main()
{
   char a[100], b[100];
    printf("Enter the first string\n");
   gets(a);
   printf("Enter the second string\n");
   gets(b);

   if( strcmp(a,b) == 0 )
      printf("Entered strings are equal.\n");
   else
      printf("Entered strings are not equal.\n");

   return 0;
}

C program to compare two strings without using string functions
#include<stdio.h>

int stringCompare(char[],char[]);
int main()
{
char str1[100],str2[100];
int compare;
printf("Enter first string: ");
scanf("%s",str1);
printf("Enter second string: ");
scanf("%s",str2);
compare = stringCompare(str1,str2);

if(compare == 1)
printf("Both strings are equal.");
else
printf("Both strings are not equal");
return 0;
}

int stringCompare(char str1[],char str2[]){
int i=0,flag=0;
while(str1[i]!='\0' && str2[i]!='\0'){
if(str1[i]!=str2[i]){
flag=1;
break;
}
i++;
}
if (flag==0 && str1[i]=='\0' && str2[i]=='\0')
return 1;
else
return 0;
}

C program to compare two strings using pointers
#include<stdio.h>

int compare_string(char*, char*);
 int main()
{
    char first[100], second[100], result;
     printf("Enter first string\n");
    gets(first);
    printf("Enter second string\n");
    gets(second);
     result = compare_string(first, second);
     if ( result == 0 )
       printf("Both strings are same.\n");
    else
       printf("Entered strings are not equal.\n");

    return 0;
}

int compare_string(char *first, char *second)
{
   while(*first==*second)
   {
      if ( *first == '\0' || *second == '\0' )
         break;

     first++;
      second++;
   }
   if( *first == '\0' && *second == '\0' )
      return 0;
   else
      return -1;
}
·        Insertion

#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
char a[10];
char b[10];
char c[10];
int p=0,r=0,i=0;
int t=0;
int x,g,s,n,o;
clrscr();
puts("Enter First String:");
gets(a);
puts("Enter Second String:");
gets(b);
printf("Enter the position where the item has to be inserted: ");
scanf("%d",&p);
r = strlen(a);
n = strlen(b);
i=0;    // Copying the input string into another array
while(i <= r)
{
c[i]=a[i];
i++;
}
s = n+r;
o = p+n;    // Adding the sub-string
for(i=p;i<s;i++)
{
x = c[i];
if(t<n)
{
a[i] = b[t];
t=t+1;
}
a[o]=x;
o=o+1;
}
printf("%s", a);
getch();
}
C programming code to Insertion substring using pointer

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

void insert_substring(char*, char*, int);
char* substring(char*, int, int);

int main()
{
   char text[100], substring[100];
   int position;

   printf("Enter some text\n");
   gets(text);

   printf("Enter the string to insert\n");
   gets(substring);

   printf("Enter the position to insert\n");
   scanf("%d", &position);

   insert_substring(text, substring, position);

   printf("%s\n",text);

   return 0;
}

void insert_substring(char *a, char *b, int position)
{
   char *f, *e;
   int length;

   length = strlen(a);

   f = substring(a, 1, position - 1 );     
   e = substring(a, position, length-position+1);

   strcpy(a, "");
   strcat(a, f);
   free(f);
   strcat(a, b);
   strcat(a, e);
   free(e);
}

char *substring(char *string, int position, int length)
{
   char *pointer;
   int c;

   pointer = malloc(length+1);

   if( pointer == NULL )
       exit(EXIT_FAILURE);

   for( c = 0 ; c < length ; c++ )
      *(pointer+c) = *((string+position-1)+c);      

   *(pointer+c) = '\0';

   return pointer;
}

·        Substring

Searching the substring
#include<stdio.h>
#include<conio.h>
void main()
{
            char str[80],search[10];         
            int count1=0,count2=0,i,j,flag;
            clrscr();
           
            puts("Enter a string:");
            gets(str);
            puts("Enter search substring:");
            gets(search);
            while (str[count1]!='\0')
                        count1++;
            while (search[count2]!='\0')
                        count2++;
            for(i=0;i<=count1-count2;i++)
            {
                        for(j=i;j<i+count2;j++)
                        {
                                    flag=1;
                                    if (str[j]!=search[j-i])
                                    {
                                        flag=0;
                                       break;
                                    }
                        }
                        if (flag==1)
                                    break;
            }
            if (flag==1)
                        puts("SEARCH SUCCESSFUL!");
            else
                        puts("SEARCH UNSUCCESSFUL!");
            getch();
}

Extract the substring

First way:
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
{
char og[50],ss[50];
int x,y,st,len;
//clrscr();
printf("Enter the String: ");
scanf("%s",og);
printf("Enter the start position and length to be extracted");
scanf("%d%d",&st,&len);
for(x=st,y=0;y<len&&og[x]!='\0';x++,y++) {
ss[y]=og[x];
}
ss[y]='\0';
printf("Your Substring is %s",ss);
getch();
}
Second way: using Pointer's
#include<conio.h>
#include<stdio.h>
#include<string.h>
void extract(char *,char *,int ,int );
void main()
{
char og[50],ss[50];
int x,y,st,len;
//clrscr();
printf("Enter the String: ");
scanf("%s",og);
printf("Enter the start position and length to be extracted");
scanf("%d%d",&st,&len);
extract(og,ss,st,len);
printf("Your Substring is %s",ss);
getch();
}

void extract(char *str1,char *str2,int s,int l)
{
int i;
str1=str1+(s-1);
for(i=0; i < l && str1 != '\0' ;i++)
{
*str2++=*str1++;
}
*str2='\0';
}

·         Deletion

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

void remove(char[],int,int);
main()
{
char a[80];
int s,e;
clrscr();
printf("Enter a string\n");
gets(a);
printf("Enter start\n");
fflush(stdin);
scanf("%d",&s);
printf("Enter end\n");
scanf("%d",&e);
remove(a,s,e);
getch();
return 0;
}
void remove(char a[],int s,int e)
{
int i;
while(a[i]!='\0')
{
for(i=0;i<s&&a[i];i++)
printf("%c",a[i]);
for(i=s;i<=e&&a[i];i++);
for(;a[i]!='\0';i++)
printf("%c",a[i]);
}
}

Delete substring at given position
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
{
      int len1,len2,i,cntr1,cntr2,flag=0;
      char str1[50],str2[50];
      clrscr();
      printf("Enter the main String and the substring to be deleted:\n");
      scanf("%s%s",str1,str2);
      len1=strlen(str1);
      len2=strlen(str2);
      for(i=0;len2<=len1-i;i++)
      {
            for(cntr1=i,cntr2=0;cntr2<=len2&&str1[cntr1]==str2[cntr2];cntr1++,cntr2++);
            if(len2<=cntr2)
            {
                  flag=1;
                  break;
            }
      }
      if(flag==0)
      printf("%s substring is not present in string %s so cannot be deleted",str2,str1);
      else
      {
            for(;(i+len2+1)<=len1;i++)
            str1[i]=str1[len2+i];
            str1[i]='\0';
      }
      printf("\nSubstring %s has been deleted and resultant string is :  %s",str2,str1);
getch();
}
Delete substring using pointer
#include "stdio.h"
#include "conio.h"
#include "string.h"
void delchar(char *x,int a, int b);
void main(){
char string[10];
int n,pos,p;
clrscr();
puts("Enter the string");
gets(string);
printf("Enter the position from where to delete");
scanf("%d",&pos);
printf("Enter the number of characters to be deleted");
scanf("%d",&n);
delchar(string, n,pos);
getch();
}
void delchar(char *x,int a, int b){
if ((a+b-1) <= strlen(x)){
strcpy(&x[b-1],&x[a+b-1]);puts(x);
}
}