String to Upper Case Letters

code:

#include<stdio.h>

void main()
{
    int i=0,j=0, length;
    char str[100];

    printf("Enter string : ");
    gets(str);

    for(i=0; str[i]!='\0';i++)
    {}
    length = i ;

    for(j=0; j<length; j++)
    {
        if(str[j]==32)//ascii of space
        {
            continue;//skip iteration to skip space character
        }
        else if(str[j]>=97 || str[j]<=122 )
        {
            str[j]=str[j]-32;
        }
    }
    printf("String in upper case is : ");
    puts(str);

}

Strcmp - String Compare

Program to compare two strings are equal or not.

Code :

#include <stdio.h>
#include <stdlib.h>
void stringcompare(char arr[100], char arr2[100]);

int main()
{
    char arr[100], arr2[100];
    stringcompare(arr,arr2);
}

void stringcompare(char arr[100], char arr2[100])
{
    int ans,length,result;
    printf("\nEnter string : ");
    gets(arr);
    printf("\nEnter 2nd String: ");
    gets(arr2);
    int i,counter=0,j,lenght,lenght2;
    for(i=0;arr[i]!='\0';i++)
    {
    }
    lenght=i;
    for(j=0;arr[j]!='\0';j++)
    {
        if(arr[j]==arr2[j])
        {
            counter++;
        }
    }
    if(counter==lenght)
    {
        printf("\n\tEqual");
    }
    else{
        printf("\n\tNot Equal\n");
    }
}

strchr - Character Occurrence

Code :

#include <stdio.h>
#include <stdlib.h>
char xstrchr(char arr[100]);
int main()
{
    char arr[100];
    xstrchr(arr);
}
char xstrchr(char arr[100])
{
    char search_key;
    printf("Enter string: ");
    gets(arr);
    int i;
    printf("Enter word u want to search: ");
    scanf("%c",&search_key);
    for(i=0;arr[i]!='\0';i++)
    {
        if(arr[i]==search_key)
        {
            printf("\ncharacter is found in %d index",i+1);
            //break ;  // remove this comment if you want to search character only once
        }
    }
}

string concatenation

#include <stdio.h>
#include <stdlib.h>
char stringcat(char arr[100],char arr2[100]);
int main()
{
    char arr[100],arr2[100];
    stringcat(arr,arr2);
}
char stringcat(char arr[100],char arr2[100])
{
    int i,ans,ans2,result,counter=0;
    printf("Enter arr\n");
    gets(arr);
    for(i=0;arr[i]!='\0';i++)
    {
    }
    ans=i;
    printf("The length of arr 1 is=%d\n",ans);
    printf("Enter 2nd arr\n");
    gets(arr2);
        for(i=0;arr2[i]!='\0';i++)
    {
    }
    ans2=i;
    printf("The length of arr 2 is=%d\n",ans2);
    result=ans+ans2;
    char arr3[result];
    arr3[result];
    for(i=0;i<=result;i++)
    {
        arr3[i]=arr[i];

        if(i==ans||i>ans)
        {
            arr3[i]=arr2[counter];
            counter++;
        }
    }
    puts(arr3);

}

Strings Programs

In this page you will find programs of <string.h> library but without using built in functions.

Guessing game

The below code will generate a guessing game.

Logic 

Store a number in an variable.This will be the hidden number.If the user enters number which is greater or smaller then the hidden number then display an appropriate message.Limit the user to fixed number of tries so that the game can end if the user is unable to guess the hidden number.


Code : 

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

void main()

{
    int x,i,j,num,count;
    printf("Welcome...\nMax number of tries is 4");
    count=0;
    x=37;    //this is the value which is to be guessed

    for(i=1;i!=x;i)

    {
        printf("\nEnter number:");
        scanf("%d",&i);
        if(i>x)
            printf("Too Large");
        if(i<x)
            printf("To Small");
        count++;
        if(count==4)
            break;
    }

    if(count==4)

        printf("\n\nYou lose\nnumber of tries:%d",count);
    else
        printf("\n\nYou win\nnumber of tries:%d",count);

    getch();

}

Teaase your Friends with this code

The following program will keep on printing the statement which is written in printf if the user is pressign any key you can send the .exe file of this code to your friends to fool them .

#include<stdio.h>
#include<conio.h>
void main()
{
char ch[]="I AM AN IDIOT.";
char c='A';
int i=0;

  while(c)
   {
     c=getch();
     printf("%c\a",ch[i]);
     i++;
    if(i==14)//if you change the printf statement then replace this 14 with the size of you string.
        {
            printf(" ");
            i=0;
        }
  }
}

Enjoy.
Newer Posts Older Posts
© Copyright Encyclopedia of C | Designed By Code Nirvana
Back To Top