Showing posts with label String. Show all posts
Showing posts with label String. Show all posts

Thursday, July 21, 2011

Program to count occourence of each character in a given string

/*Serial No.112     [swami89.cpp]*/

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str[50],comp;    //here in place of str[50] if I write *str then
int n,i,j,count;        //program also works. [TO CHECK WHY ???]
clrscr();
printf("Enter the string==>");
scanf("%s",str);
n=strlen(str);
for(i=0;i<n;i++)
    {
    comp=str[i];
    if(comp!=' ')
      {
      count=0;
      for(j=0;j<n;j++)
        {
        if(comp==str[j])
          {
          count+=1;
          str[j]=' ';
          }
        }
      }
    else
      continue;
    printf("%c occurs %d times\n",comp,count);
    }
getch();
}

Program to print string in formatted output form


/*Serial No.111     [swami88.cpp]*/ 

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

void main()
{
int i;
char name[10]="math",a[]="aaaaaaaaaaaaaaa",c='y';
clrscr();
puts(name);
for(i=1;i<5;i++)
    {
    printf("%10.*s\n",i,name);
    }
printf("\n-----------------\n");
for(i=0;i<5;i++)
    {
    printf("%*c\n",i,c);
    }
for(i=5;i>0;i--)
    {
    printf("%*c\n",i,c);
    }
getch();
}


OUTPUT :
math
            m
          ma
        mat
      math

-----------------
y
  y
    y
      y
         y
       y
      y
    y
  y

y

Program to sort an array of names in alphabetical and reverse order

/*Serial No.108     [swami85.cpp]*/

#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
    {
    int i,j,k;
    char name[10][10],tname[10][10],temp[10];
    clrscr();
    printf("How many numbers you want to store:=");
    scanf("%d",&k);
    printf("Enter numbers for in an array:=");
       fflush(stdin);

    for(i=0;i<k;i++)
       {
       scanf("%s",name[i]);
       strcpy(tname[i],name[i]);
       }

    for(i=1;i<k;i++)
       {
       for(j=1;j<k;j++)
          {
          if(strcmpi(name[j],name[j-1])<0)
          {
          strcpy(temp,name[j-1]);
          strcpy(name[j-1],name[j]);
          strcpy(name[j],temp);
          }
          }
       }
    printf("The sorted names in alphabetical order [A to Z Format] are:\n");
    printf("\tOldlist\t\tNew list\n\n");
    for(i=0;i<k;i++)
        {
        printf("\t%s\t\t%s\n",tname[i],name[i]);
        }
    printf("\n\nThe sorted names in [Z to A Format] are:\n");
    for(i=k-1;i>=0;i--)
        {
        printf("\t%s\n",name[i]);
        }
    getch();
    }

Program to count no of blank spaces and no of words in a string

/*Serial No.103     [swami80.cpp]*/

#include<stdio.h>
#include<conio.h>
#include<string.h>
int xstrlen(char *);
void main()
{
char s1[20];
int len;
clrscr();
printf("\nEnter the string:");
gets(s1);
len=xstrlen(s1);
printf("\nlength of the string =%d",len);
getch();
}
int xstrlen(char *s)
    {
    int l=0,m=0;
    while(*s)
      {
      if(*s==' ')
        {
        m++;
        }
      l++;
      s++;
      }
      printf("\nNo. of the blank spaces=%d",m);
      printf("\nNo. of the words=%d",m+1);
      printf("\n[--only if words are saparated by single space--]");
    return (l);
    }

Wednesday, July 20, 2011

Program to find length of a string including blank spaces,tabs, and other special characters

/*Serial No.100     [swami75.cpp]*/ 

/*New line character will be taken as a string terminating character.*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
int xstrlen(char *);
void main()
{
char s1[20];
int len;
clrscr();
printf("\nEnter the string:");
gets(s1);
len=xstrlen(s1);
printf("\nlength of the string =%d",len);
getch();
}
int xstrlen(char *s)
    {
    int l=0;
    while(*s)
      {
      l++;
      s++;
      }
    return (l);
    }

Program to accept a string and print the rightmost 'n' characters using function {Version 2}

/*Serial No.98     [swami73.cpp]*/ 

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<alloc.h>
char s1[20];
int n,l,i;

void pri();
void main()
{
clrscr();
printf("enter the string s1:");
gets(s1);
printf("enter the no of characters to extract=");
scanf("%d",&n);
printf("\n\noutput:\n");
pri();
getch();
}
void pri()
    {
    l=strlen(s1);
    i=l-n-1;
    while(s1[i]!='\0')
        {
        i++;
        printf("%c",s1[i]);
        }
    }

Program to accept a string and print the rightmost 'n' characters using function {Version 1}

/*Serial No.97     [swami72.cpp]*/

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<alloc.h>
char *rightsub(char *,int n);
void main()
{
char s1[20],s2[20],ch,*s;
int n;
clrscr();
printf("enter the string s1:");
gets(s1);
printf("enter the no of characters to extract=");
scanf("%d",&n);
s=rightsub(s1,n);
printf("\nright sub string: %s",s);
free(s);
getch();
}
char *rightsub(char *str,int n)
    {
    char *t=(char *)malloc(n+1);
    int l=strlen(str);
    char *s=str+(l-n);
    int i=0;
    while(i<n)
        {
        t[i]=*s;
        s++;
        i++;
        }
    t[i]='\0';
    return t;
    }

Program to count no. of vowels in a string

/*Serial No.60     [swami33.cpp]*/

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int count=0,j;
char str[20];
clrscr();
printf("Enter the string==>");
scanf("%s",str);
for(j=0;str[j]!='\0';j++)
{
if(str[j]=='a'||str[j]=='e'||str[j]=='i'||str[j]=='o'||str[j]=='u'||str[j]=='A'||str[j]=='E'||str[j]=='I'||str[j]=='O'||str[j]=='U')
{
count++;
}
}
printf("\nyour total vowels in %s is== %d",str,count);
getch();
}

Program to check whether the two strings are same or not

/*Serial No.59     [swami32.cpp]*/

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int i,j=0,flag;
char str1[10],str2[10];
clrscr();
printf("Enter the first String==>");
scanf("%s",str1);
printf("Enter the Second String==>");
scanf("%s",str2);
for(i=0;str1[10]!='\0'||str2[j]!='\0';i++,j++)
{
if(str1[i]==str2[j])
flag=1;
break;
}
if(flag==1)
printf("\nBoth Strings %s and %s are same",str1,str2);
else
printf("\nBoth Strings %s and %s are not same",str1,str2);
getch();
}

Program to reverse the string {using Pointer}

/*Serial No.55     [swami27.cpp]*/

#include<stdio.h>
#include<conio.h>
void reverse(char *);
void main()
{
clrscr();
  char st[10];
  gets(st);
  printf("reverse=");
  reverse(st);
  printf("\noriginal data=%s",st);
  getch();
}
void reverse(char *s)
{
  char c;
  if (*s==NULL)
  {
     return;
  }
  c=*s;
  s++;
  reverse(s);
  printf("%c",c);
}

Program to find the length, reverse of a string and to check it is palindrome or not

/*Serial No.52     [swami24.cpp]*/

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int i,length=0,flag;
char str[10],rev[10];
clrscr();
printf("Enter the string==>");
scanf("%s",str);
for(i=0;str[i]!='\0';i++)
{
length++;
}
printf("\nLength of %s is ==>%d\n",str,length);
length--;
for(i=0;length>=0;i++)
{
 rev[i]=str[length];
 length--;
}
rev[i]='\0';
printf("\nReverse of %s is ==>%s\n",str,rev);

 length++;
for(i=0;rev[i]!='\0';i++)
{
length++;
}
printf("\nLength of %s is ==>%d\n",rev,length);

for(i=0;str[i]!='\0'||rev[i]!='\0';i++)
{
if(str[i]!=rev[i])
    {
    flag=1;
    break;
    }
}
if(flag==1)
printf("\nString is not polyndrom");
else
printf("\nString is polyndrom");
getch();
}

Program to check whether the string is palindrome or not {using library functions}

/*Serial No.49     [swami21.cpp]*/

#include<stdio.h>
#include<conio.h>
#include<string.h>
#define size 26
void main()
{
char strsrc[size];
char strtmp[size];
clrscr();
printf("\n Enter String:= "); gets(strsrc);
strcpy(strtmp,strsrc);
strrev(strtmp);
if(strcmp(strsrc,strtmp)==0)
printf("\n Entered string \"%s\" is palindrome",strsrc);
else
printf("\n Entered string \"%s\" is not palindrome",strsrc);
getch();
}

Tuesday, July 19, 2011

Program to find length of string and concanate two strings using function by passing them as parameter

/*Serial No.117     [swami94.cpp]*/

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

int leng(char str[20]);
void concat(char str4[20], char str5[20]);
void main()
{
char str1[20],str2[20],str3[20];
int i,j,len;
clrscr();
printf("Enter the first string:\n");
gets(str1);

printf("Enter the second string:\n");
gets(str2);

concat(str1,str2);

len=leng(str1);
printf("\n\n\nlength of first string using return=%d",len);

getch();
}

int leng(char str[20])
    {
    int i=0;
    while(str[i]!='\0')
    {
    i++;
    }
printf("\n\n\nlength of first string=%d",i);
return(i);
    }

void concat(char str4[20], char str5[20])
    {
    int i=0,j;
    char str6[20];
    while(str4[i]!='\0')
        {
        str6[i]=str4[i];
        i++;
        }
    j=0;
    while(str5[j]!='\0')
        {
        str6[i]=str5[j];
        i++;j++;
        }
    str6[i]='\0';
    printf("\n\nConcanate string= %s",str6);
    }

Program to concanate two strings using pointer and function by passing strings as parameter

/*Serial No.120     [swami97.cpp]*/

#include<stdio.h>
#include<conio.h>
#include<string.h>
void concat(char *t, char *p);
void main()
{
char str1[10],str2[10];
int l;
clrscr();
printf("Enter the string==>");
gets(str1);
printf("Enter the string==>");
gets(str2);
concat(str1,str2);
getch();
}
void concat(char *s1, char *s2)
    {
    char new_str[20];
    int i=0;
    while(*s1)
        {
        new_str[i]=*s1;
        *s1++;
        i++;
        }
    while(*s2)
        {
        new_str[i]=*s2;
        s2++;
        i++;
        }
    new_str[i]='\0';
    puts(new_str);
    }

Program to find length of string and concanate two strings

/*Serial No.121     [swami100.cpp]*/

/* C program which read two strings and concatenate the largest string to the smallest string (among the two strings).*/
#include<stdio.h>
#include<string.h>
#include<conio.h>

int leng(char str[20]);
void concat(char str4[20], char str5[20]);
void main()
{
char str1[20],str2[20],str3[20];
int i,j,len,first,second;
clrscr();
printf("Enter the first string:\n");
gets(str1);

printf("Enter the second string:\n");
gets(str2);
first=leng(str1);
second=leng(str2);
printf("\nLength of 1st string= %d",first);
printf("\nLength of 2nd string= %d",second);

if(first<=second)
    {
    concat(str1,str2);
    }
else
    {
    concat(str2,str1);
    }
getch();
}

int leng(char str[20])
    {
    int i=0;
    while(str[i]!='\0')
        {
        i++;
        }
    return(i);
    }

void concat(char str4[20], char str5[20])
    {
    int i=0,j;
    char str6[20];
    while(str4[i]!='\0')
        {
        str6[i]=str4[i];
        i++;
        }
    j=0;
    while(str5[j]!='\0')
        {
        str6[i]=str5[j];
        i++;j++;
        }
    str6[i]='\0';
    printf("\n\nConcanated string= %s",str6);
    }

Program to print lines of a paragraph having more than 15 characters.

/*Serial No.158     [swami139.cpp]*/

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

void main()
{
int i=0,n=0;
char c,s[500],temp[100];
clrscr();
printf("\nEnter the paragraph, to stop writing press (CTRL+Z)\n");
/*following code will stop reading after pressing Ctrl+Z :*/
while((c=getch())!='\x1A')
    {
    if(c=='\r')
        {
        printf("\n");
        s[i++]='\n';
        }
    else
        {
        printf("%c",c);
        s[i++]=c;
        }
    }
/*choosing lines with more than 15 characters.*/
for(i=0;s[i]!='\0';i++)
    {
    temp[n]=s[i];
    n++;
    if(s[i]=='\n')
        {
        temp[n]='\0';
        if(n>15)
        printf("\n\t%s",temp);
        n=0;
        }
    }
    if(n>15)
        {
        temp[n]='\0';
        printf("\n\t%s",temp);
        }
getch();
}

Program to convert all lowere case char to upper & vice versa also print all in upper & in lower

/*Serial No.156     [swami137.cpp]*/

#include<stdio.h>
#include<conio.h>
void main()
{
char str1[20];
clrscr();
printf("\nEnter first string= ");
scanf("%[^\n]",str1);
/**to convert upper to lower case & vice versa**/
for(int i=0;str1[i]!='\0';i++)
    {
    if(str1[i]>=97&&str1[i]<123)
        str1[i]=str1[i]-32;
    else if(str1[i]>=65&&str1[i]<91)
        str1[i]=str1[i]+32;
    }
printf("\n\nString after convering upper to lower & vice versa=\n\t%s",str1);

/**for upper case**/
for(i=0;str1[i]!='\0';i++)
    {
    if(str1[i]>=97&&str1[i]<123)
        str1[i]=str1[i]-32;
    }
printf("\n\nString in Upper case=\n\t%s",str1);

/**for lower case**/
for(i=0;str1[i]!='\0';i++)
    {
    if(str1[i]>=65&&str1[i]<91)
        str1[i]=str1[i]+32;
    }
printf("\n\nString in Lower case=\n\t%s",str1);
getch();
}