The Programming Project: strings
Showing posts with label strings. Show all posts
Showing posts with label strings. Show all posts

Sunday, December 16, 2012

C Program #6


/* program to write a sentence is one file and print the entire sentence
to the second file by reversing the non palindrome word only*/
#include<stdio.h>
#include<string.h>
int palin(char c[15]);
int main()
{
FILE *fp,*fq;
char c='a',ct[15];
int i,len,j;
fp=fopen("first.txt","w");
if(fp==NULL)
      {
      printf("\n ERROR opening file:");
      }
else
      {
      printf("\n Enter the text:");
      while(c!='\n')
            {
      scanf("%c",&c);
            fprintf(fp,"%c",c);
            }
      }
fclose(fp);
fq=fopen("first.txt","r");
fp=fopen("second.txt","w");
if(fq==NULL)
      {
      printf("\n ERROR opening file:");
      }
else
      {
      while(!feof(fq))
            {
                  i=0;
                  c=fgetc(fq);
                  while(c!=' ' && c!='\n')
                        {
                        ct[i]=c;
                        i++;
                        c=fgetc(fq);
                        if(c==EOF)break;
                        }ct[i]='\0';
                  if(palin(ct)==0)
                        {
                        fputs(ct,fp);
                        fputc(' ',fp);
                        }
                  else
                        {
                        len=strlen(ct);
                        for(j=len-1;j>=0;j--)
                              {
                        fputc(ct[j],fp);
                              }
                        fputc(' ',fp);
                        }

            }
      }
fcloseall();
fp=fopen("second.txt","r");
      if(fp==NULL)
                  printf("\n ERROR OPENING FILE:");
      else
                  {
         printf("\n Text is the second file:\n");
                  while(!feof(fp))
                        {
                        fscanf(fp,"%c",&c);
                        printf("%c",c);
                        }
                  }
fcloseall();
return 0;
}
int palin(char c[])
{
int len,i,j=0;
char p[15];
len=strlen(c);
for(i=len-1;i>=0;i--)
      {
      p[j]=c[i];
      j++;
      }
      p[j]='\0';
      if(strcmp(p,c)==0)
            return 0;
            else
                  return 1;

}

C Program #5



/* Program to reverse the alternate words of a string */
#include<stdio.h>
#include<string.h>
void reverse(char rev[],int n);
int main()
{
char str[100],rev[10];
int len,i,j,count;
printf("\n Enter a string:");
gets(str);
len=strlen(str);
printf("\n Converted string is:\n");
for(i=0;i<len;i++)
      {
       printf("%c",str[i]);
       if(str[i]==' ')
            {

                  j=i;
                  count=0;
                  while(str[++j]!=' ')
                        {
                        rev[count]=str[j];
                        count ++;
                        }
                        reverse(rev,count);
                        i=j;
            putc(' ',stdout);
                  }

       }
      return 0;
}
void reverse(char rev[],int n)
{
 int i;
      for(i=n-1;i>=0;i--)
            printf("%c",rev[i]);
}
INPUT/OUTPUT:
Enter a string:Program to reverse the alternate words of a string

 Converted string is:
Program ot reverse eht alternate sdrow of a string 

Saturday, December 15, 2012

Sorting of words in a String : C Code


Program to sort the words of a string

/* To sort the words of an input string */
#include<stdio.h>
#include<string.h>
#include<malloc.h>
int main()
{
char str[40],*x[10],*temp;
int i=0,j=0,len,t,k,l,tmp;
printf("\n Enter a string:");
gets(str);
len=strlen(str);
for(i=0;i<=len;i++)
      {
      k=i;
      tmp=0;
      while(str[k]!=' ' && str[k]!='\0')
            {
            k++;
            tmp++;
            if(str[i]=='\n')
            break;
            }
            x[j]=(char *)malloc(tmp*sizeof(char));
            l=0;
                  for(t=i;t<tmp+i;t++)
                  {
                        *(x[j]+l)=str[t];
                        l++;
                  }
                  *(x[j]+l)='\0';
            i=k;
            j++;
            }
for(i=0;i<j-1;i++)
      {
            for(k=i+1;k<j;k++)
                  {
                  if(strcmp(x[i],x[k])>0)
                        {
                        temp=x[k];
                        x[k]=x[i];
                        x[i]=temp;
                        }
                  }
      }
puts("\n Sorted words of the string:\n");
for(i=0;i<j;i++)
      printf("%s\n",x[i]);
return 0;
}



Input/Output:

Enter a string:sorting of the words in a string

 Sorted words of the string:

a
in
of
sorting
string
the
words