STRINGS

#include <stdio.h>
#include<string.h>
int main()
{
    int i;
    char s[i][10];
    printf("Enter the string\n");
    for(i=0;i<3;i++)
    {
        gets(&s[i]);
    }
    printf("The desired values are\n");
    for(i=0;i<3;i++)
    {
        puts(s[i]);
    }
    return 0;
}

output:


Enter the string

India

America

Sweden

The desired values are

India

America
Sweden





program to print a string in reverse order

#include <stdio.h>
#include<string.h>
int main()
{
    char s[20],ch;int i;
    printf("Enter any string\n");
    gets(s);
    int k= strlen(s);
    printf("Length of the string is %d\n",k);
    for( i=0;i<k/2;i++)
    {
        ch = s[i];
        s[i]=s[k-i-1];
        s[k-i-1]= ch;
    }
    
    printf("reverse of a string is %s",s);
    
}
OUTPUT:
Enter any string
HACKER
Lenght of the string is 6
reverse of a string is REKCAH



program to print reverse of a string when spaces are included

#include <stdio.h>
#include<string.h>
int main()
{
    char s[50];
    printf("Enter a string\n");
    gets(s);
    reverse(s);
    puts(s );
}

void reverse(char s[])
{
    int length = strlen(s);
    for(int i=length-1;i>=0;i--)
    {
        if(s[i]==' ')
        {
            s[i]='\0';
            printf("%s ",(&s[i])+1);
        }
    }

}

OUTPUT:
Enter a string
I WILL BECOME A DEVELOPER
DEVELOPER A BECOME WILL I


Converting a String into upper & lower case & reversing it

#include <stdio.h>
#include<string.h>
int main()
{
    char s[50];
    printf("Enter a string\n");
    gets(s);
    reverse(s);
    puts(s );
}

void reverse(char s[])
{
    int length = strlen(s);
    for(int i=length-1;i>=0;i--)
    {
        if(s[i]>='a'&& s[i]<='z')
        {
        s[i]= s[i]-32;
        }
        else if(s[i]>='A'&&s[i]<='Z')
        {
            s[i]=s[i]+32;
        }
        if(s[i]==' ')
        {
            s[i]='\0';
            printf("%s ",(&s[i])+1);
        }
    }
    

}

OUTPUT:
Enter a string
Hello World
wORLD hELLO



Check whether the string is palindrome or not

#include <stdio.h>
#include<string.h>
int main()
{
    char s[20];int i;
    printf("Enter any string\n");
    gets(s);
    int k= strlen(s);
    printf("Length of the string is %d\n",k);
    for( i=0;i<k/2;i++)
    {
        if(s[i]!=s[k-i-1])
        {
            printf("Given string is a not a palindrome");
            break;
        }
    }
    if(i==k/2)
    {
        printf("The given String is a palindrome");
    }
    
}

OUTPUT:
Enter any string
madam
Length of the string is 5
The given String is a palindrome


Finding a Substring in a String:
#include <stdio.h>
#include<string.h>
int subString(char[],char[]);
int main()
{
    int index;
  index= subString("abababbbaabbababbbaa","aabb");
   if(index==-1)
   {
       printf("substring is not found");
   }
   else
   {
   printf("substring is found at index %d",index);
   }
}
int subString(char s[],char sub[])
{
    int l,j,k,i;
    l = strlen(sub);
    for( i=0;i<s[i];i++)
    {
         k =i;
        for( j=0;j<=l-1;j++)
        {
            if(s[k]!=sub[j])
            break;
        
         k++;
        }
       if(j==l)
       {
       return(i);
       }
           
    }
    return -1;
}

OUTPUT:
Counting number of vowles in a string

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

int main()
{
    char s1[11]="aeiouAEIOU",s[50];
    int length= strlen(s1),count=0;
    printf("enter any string\n");
    gets(s);
    for(int i=0;i<s[i];i++)
    {
        for(int j=0;j<length;j++)
        {
            if(s[i]==s1[j])
            {
                count++;
                break;
            }
        }
    }
    printf("The no.of vowels are %d",count);
}

OUTPUT:
enter any string
hello

The no.of vowels are 2


Reversing a sentence and counting number of words in it

#include <stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
    char s[50];
    
    printf("Enter a string\n");
    gets(s);
    reverse(s);
    
}
void reverse(char s[])
{
    int length = strlen(s),count=0;
    for(int i=length-1;i>=0;i--)
    {
        if(s[i]==' ')
        {
             count++;
            s[i]='\0';
            printf("%s ",(&s[i])+1);
        }
    }
    puts(s);
    printf("\nNo.of words in a sentence are %d\n",count+1);

}

OUTPUT:
Enter a string
I am a Robot
Robot a am I

No.of words in a sentence are 4





Printing only unique by ignoring duplicates

#include<string.h>
#include<stdlib.h>
int main()
{
    char s[15]="Reeghevaee",s1[15];
    strcpy(s1,s);
  
    int l = strlen(s),j;
    for(int i=0;i<l;i++)
    {
        for(j =i+1;j<l;j++)
        {
            if(s[i]==s1[j]||s[i]==s[i-1])
            break;
         
        }
        if(j==l)
        {
  
            printf("%c",s[i]);
        }
    }

}
OUTPUT:



Printing Strings in Dictionary Order:

#include <stdio.h>
int main()
{
    char str[5][10],s[10];
    int i,j,r;
    printf("Enter five names\n");
    for(i=0;i<=4;i++)
    {
        gets(str[i]);
    }
    for(j=1;j<=4;j++)
    {
        for(i=0;i<4-j;i++)
        {
        
            r= strcmp(str[i],str[i+1]);
            if(r>0)
            {
                strcpy(s,str[i]);
                strcpy(str[i],str[i+1]);
                strcpy(str[i+1],s);
        
            }
        }
    }
    printf("\nstrings in dictonary order are\n");
    for(i=0;i<=4;i++)
    {
        printf("%s",str[i]);
        printf("\n");
    }
    return 0;
OUTPUT:
Enter five names
light
sand
fire
space
wind

strings in dictonary order are
fire
light
sand
space
wind


Comments

Popular posts from this blog

Program to know the range of short signed integer

Finding the second largest in an array