Computer Scientists are Pretty Pessimistic

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

Sunday, 4 October 2015

WAP to add space before an Uppercase letter in a string, ignore the first alphabet.

Input :
ILoveMyCountry
ImtiyazHossainHira

Output :
I Love My Country
Imtiyaz Hossain Hira

CODE ::

#include <stdio.h>
#include <string.h>
int main ()
{
    char a[1000],b[1000];
    int i,j;
    gets(a);
    b[0]=a[0];
    for(i=1,j=1;i<a[i];j++,i++)
    {
        if(a[i]>='A'&&a[i]<='Z')
        {
            b[j]=' ';
            b[j+1]=a[i];
            j++;
        }
        else
            b[j]=a[i];
    }
    b[j]='\0';
    puts(b);
    return 0;

}

WAP convert upper letter to lower & lower letter to upper.

CODE ::

#include <stdio.h>
int main ()
{
    char a[1000];
    int i;
    printf("Enter a sentence: ");
    gets(a);
    for(i=0;i<a[i];i++)
    {
        if(a[i]>='A'&&a[i]<='Z')
            printf("%c",a[i]-'A'+'a');
        else
            printf("%c",a[i]-'a'+'A');
    }
    return 0;
}

Thursday, 1 October 2015

WAP to input a multi word string and produce a string in which first letter of each word is capitalized.

CODE ::

#include <stdio.h>
#include <string.h>
int main ()
{
    char c[100];
    int i;
    printf("Enter string: ");
    gets(c);
    for(i=0;i<c[i];i++)
    {
        if(c[0]>='a'&&c[0]<='z')
            c[0]=c[0]-'a'+'A';

        if(c[i]==' '||c[i]=='.'||c[i]==','||c[i]=='?'||c[i]=='!'||c[i]==';'||c[i]==':')
        {
            if(c[i+1]>='a'&&c[i+1]<='z')
                c[i+1]=c[i+1]-'a'+'A';
        }
        printf("%c",c[i]);
    }
    return 0;
}

WAP to count the number of words and number of characters in a given line of text except the spaces.

CODE ::

#include <stdio.h>
int main ()
{
    int w=1,l=0,i;
    char c[100];
    printf("Enter string: ");
    gets(c);
    for(i=0;i<c[i];i++)
    {
        if(c[i]>='a'&&c[i]<='z'||c[i]>='A'&&c[i]<='Z') //count letter
            l++;

        if(c[i]==' '||c[i]=='.'||c[i]==','||c[i]=='?'||c[i]=='!'||c[i]==';'||c[i]==':')
        {
            if(c[i+1]>='a'&&c[i+1]<='z'||c[i+1]>='A'&&c[i+1]<='Z')
                w++;
        }
    }
    printf("Words: %d\nLetters: %d",w,l);
    return 0;
}

WAP to input a character and a string. Each occurrence of a character in the string should be converted to opposite case i.e. upper to lower case or vice versa.

CODE ::

#include <stdio.h>
int main ()
{
    char a[100],c;
    int i,j;
    printf("Enter string: ");
    gets(a);
    fflush (stdin);
    printf("Enter a character which one you want to change: ");
    scanf("%c",&c);
    for(i=0;i<a[i];i++)
    {
        if(a[i]==c)
        {
            if(a[i]>='A'&&a[i]<='Z')
                a[i]=a[i]-'A'+'a';

            else if (a[i]>='a'&&a[i]<='z')
                a[i]=a[i]-'a'+'A';
        }
        printf("%c",a[i]);
    }
    return 0;
}

WAP to search a character in a given string.

#include <stdio.h> // This code is case sensitive
int main ()
{
    int i;
    char a[100],c,flag=0;
    printf("Enter a string: ");
    gets(a);
    printf("Enter a character to find: ");
    scanf("%c",&c);
    for(i=0;i<a[i];i++)
    {
        if(a[i]==c)
        {
            flag=1;
            break;
        }
    }
    if(flag==1)
        printf("FOUND\n");
    else
        printf("NOT FOUND\n");

    return 0;
}

WAP to search a given string into another string and displays the position if found otherwise displays 0.

CODE ::

#include <stdio.h>      //This code is case sensitive
#include <string.h>
int main ()
{
    char a[100],b[100];
    int i,w=1,k,l=0,j;
    printf("Enter string: ");
    gets(a);
    printf("Enter substring: ");
    gets(b);
    for(i=0;i<a[i];i++)
    {
        if(a[i]==' '||a[i]=='.'||a[i]==','||a[i]=='?'||a[i]=='!'||a[i]==';'||a[i]==':')
        {
            if(a[i+1]>='a'&&a[i+1]<='z'||a[i+1]>='A'&&a[i+1]<='Z')
                w++;
        }

        if(a[i]==b[0])
        {
            for(k=i,j=0;j<b[j];k++,j++)
            {
                if(a[k]==b[j])
                    l++;
            }
        }
        if(l==strlen(b))
            break;
    }
    if(l==strlen(b))
        printf("\nSub-String found in position %d\n",w);
    else
        printf("0");
    return 0;
}

WAP to extract specified number of characters from a given position from a string.

CODE ::

#include <stdio.h>
int main ()
{
    int i,n;
    char a[100];
    printf("Enter string: ");
    gets(a);
    printf("Enter character position: ");
    scanf("%d",&n);
    for(i=0;i<a[i];i++)
    {
        if((i+1)==n)
        {
            printf("The value of this character %d",a[i]);
            break;
        }
    }
    return 0;
}

WAP to count all the occurrences of a character in a given string.

CODE ::

#include <stdio.h>
#include <string.h>
int count [26];
int main ()
{
    char a[100];
    int i,j;
    printf("Enter string: ");
    gets(a);
    for(i=0;i<a[i];i++)
    {
        if(a[i]>='A'&&a[i]<='Z')
        {
            a[i]=a[i]-'A'+'a';
            count [a[i]-'a']++;
        }
        else if(a[i]>='a'&&a[i]<='z')
            count[a[i]-'a']++;
    }
    for(i=0;i<a[i];i++)
    {
        if(count[i]>0)
            printf("%c = %d\n",(i+'a'),count[i]);
    }
    return 0;
}

WAP to copy a string into another string.

CODE ::

#include <stdio.h>
#include <string.h>
int main ()
{
    int i,j,k;
    char a[100],b[100],c[100];
    printf("Enter a string: ");
    gets(a);
    printf("Enter another string: ");
    gets(b);
    for(i=0;i<a[i];i++)
    {
        c[i]=a[i];
    }
    c[i++]=' ';
    for(j=i,k=0;k<b[k];k++,j++)
    {
        c[j]=b[k];
    }
    c[j]='\0';
    puts(c);
    return 0;
}

WAP to concatenate two strings.

CODE ::

#include <stdio.h>
#include <string.h>
int main ()
{
    int i,j,k;
    char a[100],b[100],c[100];
    printf("Enter a string: ");
    gets(a);
    printf("Enter another string: ");
    gets(b);
    for(i=0;i<a[i];i++)
    {
        c[i]=a[i];
    }
    c[i++]=' ';
    for(j=i,k=0;k<b[k];k++,j++)
    {
        c[j]=b[k];
    }
    c[j]='\0';
    puts(c);
    return 0;
}


Saturday, 5 September 2015

WAP to replace every space in a string with a hyphen, tab with a hash and digit with a slash.

#include <stdio.h>
#include <string.h>
int main ()
{
    char a[100];
    int i;
    printf("Enter a string: ");
    gets(a);
    for(i=0;i<a[i];i++)
    {
        if(a[i]==' ')
            a[i]='-';
        else if(a[i]=='\t')
            a[i]='#';
        else if(a[i]>='0'&&a[i]<='9')
            a[i]='\\';
        printf("%c",a[i]);
    }
    return 0;
}

WAP to compare two strings

#include <stdio.h>
#include <string.h>
int main ()
{
    int i,j,m=0;
    char a[100],b[100];
    printf("Enter a string: ");
    gets(a);
    printf("Enter another string: ");
    gets(b);
    for(i=0;i<a[i];i++)
    {
        if(a[i]==b[i])
            m++;
    }
    if(m==strlen(a))
        printf("\nMatched\n");
    else
        printf("\nNot Matched\n");

    return 0;
}

WAP to copy a string into another string.

#include <stdio.h>
int main ()
{
    int i,j;
    char a[100],b[100];
    printf("Enter a string: ");
    gets(a);
    printf("Enter another string: ");
    gets(b);
    for(i=0;i<b[i];i++)
    {
        a[i]=b[i];
    }
    for(i=0;i<b[i];i++)
    {
        printf("%c",a[i]);
    }
    return 0;
}

WAP to extract specified number of characters from a given position from a string.

#include <stdio.h>
int main ()
{
    int i,n;
    char a[100];
    printf("Enter string: ");
    gets(a);
    printf("Enter character position: ");
    scanf("%d",&n);
    for(i=0;i<a[i];i++)
    {
        if((i+1)==n)
        {
            printf("The value of this character %d",a[i]);
            break;
        }
    }
    return 0;
}

WAP to search a character in a given string.

#include <stdio.h> // This code is case sensitive
int main ()
{
    int i;
    char a[100],c,flag=0;
    printf("Enter a string: ");
    gets(a);
    printf("Enter a character to find: ");
    scanf("%c",&c);
    for(i=0;i<a[i];i++)
    {
        if(a[i]==c)
        {
            flag=1;
            break;
        }
    }
    if(flag==1)
        printf("FOUND\n");
    else
        printf("NOT FOUND\n");

    return 0;
}

Friday, 4 September 2015

WAP to count the number of spaces, tabs and new line characters in a given string.

#include <stdio.h>
int main ()
{
    char ch[100];
    int s=0,t=0,nl=0,i;
    printf("Enter String: ");
    gets(ch);
    for(i=0;i<ch[i];i++)
    {
        if(ch[i]==' ')
            s++;
        else if(ch[i]=='\t')
            t++;
        else if(ch[i]=='\n')
            nl++;

    }
    printf("Spaces : %d\nTabs : %d\nNew line : %d",s,t,nl);
    return 0;
}