3/19/2016

Char and string

1.A C program for counts frequency of letters in given string.


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

int main()
{
   char string[100];
   int c = 0, count[26] = {0};

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

   while ( string[c] != '\0' )
   {
   

      if ( string[c] >= 'a' && string[c] <= 'z' )
   count[string[c]-'a']++;

      c++;
   }

   for ( c = 0 ; c < 26 ; c++ )
   {
      if( count[c] != 0 )
         printf("%c occurs %d times in the entered string.\n",c+'a',count[c]);
   }

 getch();
}


Output:
Enter a string
aabbccc
a occurs 2 times in the entered string.
b occurs 2 times in the entered string.
c occurs 3 times in the entered string.

2.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);
/* strlen(string) : function gives length of string*/

  printf("Length of entered string is = %d\n",length);
 return 0;
}
Output:
Enter a string to calculate it's length
welcome
Length of entered string is = 7


3.Compare strings

#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);
/*strcmp(string, string) : returns 0 if strings are equal else strings are unequal*/
  if( strcmp(a,b) == 0 )
  printf("Entered strings are equal.\n");
  else
  printf("Entered strings are not equal.\n");
return 0;

}

Output:
Enter the first string
hello
Enter the second string
hello
Entered strings are equal.

4.Copy string

#include<stdio.h>
#include<string.h>
main()
{
  char source[] = "C program";
  char destination[50];

  strcpy(destination, source);
/*strcpy will copy string from source to destination */

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

  return 0;
}

Output:
Source string:C program
Destination string:C prograram




5.Concatenate strings

#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);

  strcat(a,b);
/*strcat will append string b on string a*/

  printf("String obtained on concatenation is \n%s\n",a);

  return 0;
}

Output:
Enter the first string
Ram
Enter the second string
Raj
String obtained on concatenation is 
RamRaj



6.A C program to check either a string is palindrome or not.
Note: Palindromes are words or phrases that read the same in both directions.


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

char *strrev(char *str)
{
      char *p1, *p2;

      if (! str || ! *str)
            return str;
      for (p1 = str, p2 = str + strlen(str) - 1; p2 > p1; ++p1, --p2)
      {
            *p1 ^= *p2;
            *p2 ^= *p1;
            *p1 ^= *p2;
      }
      return str;
}

int main()
{
char a[100], b[100];

//Get the number from user.
printf("Enter the string to check if it is a palindrome\n");
gets(a);

//copy input to another array b
strcpy(b,a);

//Reverse it.
strrev(b);

/*Compare it with the number entered by the user.If both are same then print palindrome number*/
if( strcmp(a,b) == 0 )
printf("\nEntered string is a palindrome.\n");
else
printf("\nEntered string is not a palindrome.\n");
getch();
return 0;
}

Output:
Enter a string to check if it is a palindrome
radar

Entered string is a paindrome.



7.A C++ program to change the string in capital letter into small letter.

#include<iostream>
using namespace std;
int main()
{
int i;
char name[100];
char ch;
cout<<"Enter a word in capital letter\n";
cout<<"Enter @ at the last\n";
cin>>name;
while(name[i] != '@')
{
ch=name[i];
ch=ch+32;
cout<<ch;
i++;
}
return 0;
}

Enter a word in capital letter
Enter @ at the last
TEST@
test


8.A C++ program for find the length of a string and reverses the string.


#include<iostream>
#include<conio.h>
#include<stdio.h>
#include<string.h>
using namespace std;

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

for(l=0;str[l]!='\0';l++);  
 
    cout<<"Length of string:"<<l<<endl;

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;

return 0;

}


Output:
Enter string:welcome
Length of string:7
Reverse string is
emoclew



2 comments:

  1. I have a question:-
    Write a C program which accept a string and write the string without vowel.
    Please give answer.

    ReplyDelete