Pages

Custom Search
Showing posts with label string. Show all posts
Showing posts with label string. Show all posts

Saturday, August 9, 2014

Program to convert string to title case

Program to convert string to title case.
In this program we are going to convert this first letter of every word in the string to UPPER CASE.
and all other letters to lower case.
This is simple but interesting program.

The LOGIC part:
Logic of the program is very simple.
Character by character parse the string from start to end of string.
Check if each letter is in proper case. If not convert it to the proper case.
SOURCE CODE:
 #include <stdio.h>  
 #include <stdlib.h>  
 int main()  
 {  
      char str[50];  
      int i, j, len;  
      printf("\nEnter a string: ");  
      gets(str);  
      printf("\nEntered string: ");  
      puts(str);  
      len=strlen(str);  
      i=0;  
      while(i!=len)                                   //til end of string  
      {  
           if((i==0))                                   //first letter of string  
           {  
                if(str[i]>90)                         //if it is not UPPER case  
                     str[i] = str[i]%65+33;  
                i++;  
           }  
           else if((str[i]!=' ') && (str[i]<=90))          //any other position than first letter of word  
           {  
                str[i]=str[i]%97+32;  
                i++;  
           }  
           else if(str[i]==' ')                    //for first letter of a word  
           {  
                i++;  
                if(str[i]>96)  
                     str[i]=str[i]%65+33;  
                i++;  
           }  
           else  
                i++;                                   //if letter is in correct case, simply increment i  
      }  
      str[i]='\0';  
      printf("\nOutput: ");  
      puts(str);  
      printf("\n\n");  
      return 1;  
 }  


SAMPLE OUTPUT

Program to reverse the order of words in a string

Program to reverse the order of words in a string.
For Eg:
Input: this is a simple c program
Output: program c simple a is this

The LOGIC part:
-First determine the length of the string. Use a simple for loop
until you find '\0' else use an inbuilt string function 'strlen()' like I have used.
-then move over to the last word in your string.
Place a pointer at the end of the last word and then find the
space before that word and place a pointer after the space
and using these two pointer append the word to a new string.
-Repeat procedure for all words from last word to first word.


SOURCE CODE:

 #include <stdio.h>  
 #include <stdlib.h>  
 int main()  
 {  
      char str[100], rev[100];  
      int i, j, k, m, n, len, c=0, flag=1;  
      printf("\nEnter a sentence: ");  
      gets(str);  
      printf("\nEntered sentence: ");  
      puts(str);  
      len = strlen(str)-1;          //length of string except \0  
      i=0;  
      j=len;  
      k=0;  
      while(flag==1)                    //while more words to process  
      {  
           flag = 1;  
           i=j;                         //pointer i, j to end of current word  
           while(str[i] != ' ')     //decrement pointer i till beginning of current word  
           {  
                if(i==0)  
                {  
                     flag = 0;          //special case for 1st word of the sentence  
                     break;  
                }  
                i--;  
           }  
            m=i;  
           if(flag)  
           {  
                i=i+1;       
           }       
           for( ; i<=j; )               //append current word to new char array  
           {  
                rev[k++] = str[i++];  
           }  
           j=m-1;                         //move pointer j to end of previous word  
           rev[k++]=' ';  
      }  
      rev[k]='\0';  
      printf("\nReverse sentence: ");  
      puts(rev);  
      printf("\n");  
      return 1;  
 }  


SAMPLE OUTPUT:

Monday, October 10, 2011

Program to check if entered string is palindrome or not using stack.


/*A string is said to be palindrome when the reverse of the string is same as that of the original string
   For ex: Entered string: madam... is a palindrome*/

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define SIZE 10

typedef struct
{
        int items[SIZE];
        int top;

}STACK;


void push();
int pop();
void display();
int isoverflow();
int isempty();

int main()
{
        STACK s;
        char str[100];
        int i, flag;

        s.top = -1;

        printf("\nEnter a string: ");
        gets(str);

        for(i=0;str[i]!='\0'; i++)
                  push(&s, str[i]);

        flag = 1;
        for(i=0;str[i]!='\0';i++)
        {
                if(str[i] != pop(&s))
                {
                        flag = 0;
                        break;
                }
        }

if(flag == 1)
                printf("\nEntered string is palindrome\n");
        else
                printf("\nEntered string is not a palindrome\n");

}

void push(STACK *p, int element)
{
           if(isoverflow(p->top))
           {
                    printf("\nStack is overflow");
           }
           else
          {
                  (p->top)++;
                   p->items[p->top] = element;
          }
}

int pop(STACK *p)
{
         if(isempty(p->top))
         {
                  printf("\nStack is underflow");
         }
         else
         {
                  return (p->items[(p->top)--]);
         }
}

void display(STACK s)
{
        int i;
        if(isempty(s.top))
        {
printf("\nStack is empty");
        }
        else
        {
                for(i=s.top; i>=0; i--)
                {
                        printf("\n%d", s.items[i]);
                }
        }
}

//Function to check stack overflow condition
int isoverflow(int top)
{
        if(top == SIZE - 1)
                return (1);
        else
                return (0);
}

//Function to check stack empty condition
int isempty(int top)
{
         if(top == -1)
                return (1);
        else
                return (0);
}

SAMPLE OUTPUT:




Custom Search