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

Sunday, 23 February 2014

Searching For A Character in a String - C++ Source Code

Here is another string-manipulation program. The program searches a character in a user given string, and if it finds the character, it displays the position, and if the character is not found the program displays a not-found message.

Code:
//Support us by sharing, commenting, and visiting often! :)
#include<iostream.h>
#include<string>
void main()
{
    int i;
    char a[100],b;
    cout<<"Enter the string\n";
    cin.getline(a, 50);
    cout<<"Enter the letter\n";
    cin>>b;
    int flag=0;
    for(i=0;i<strlen(a);i++)
    {
        if(b==a[i])
        {
              cout<<"Found at "<<i+1<<" :)\n";
              flag++;
        }
    }
    if(flag==0)
    {
        cout<<"Character not found! :(\n";
    }
}

For newer IDEs, visit this page.

Working:

We first input the string from the user and the character to be searched. We also declare an integer 'flag' and initially set it to 0. A for loop is initiated. If the character the user entered is same as the character the for loop is at, we output the position just after the position of the for loop (because, array usually begins at 0).
Also, the integer 'flag' is decremented. Outside the loop, if the flag is still equal to 0, the program outputs the error message, and if it is more than 1, it does nothing. Two sample outputs are given below..



Happy Coding!
:)

Saturday, 22 February 2014

Caesar Cipher - Implementation in C++ : Program Source code

This is one of my all-time favorite programs. This is the program code for one of the most popular and easiest encryption techniques in Cryptography. Hey... Don't get over-excited, because, this is not what governments are using to encrypt their secrets, but still, it's worth knowing. Let's start by understanding Caesar Cipher is.

Caesar Cipher

Like all ciphers, caesar ciphers are also used to communicate messages from a source to another, without the middleman/medium does not know about the message. Ciphers are exclusively used in wars to communicate military secrets. The field of Cryptography deals with these kind of stuff. In earlier days, encryption and decryption of messages were manual. This proved to be very inefficient, tedious, and time consuming. Then came computers. Computers aided cryptography very much. With the help of computers and computer programming, encryption, transmission, and decryption of messages became very easy.

Caesar cipher is said to be used first by Roman Emperor Julius Caesar, whose name became bound to the method with time. It is also called Caesar shift cipher, because, the method used can be described as shifting. Let's consider an example where number of letters shifted is 3. If the input letter is 'a', the output letter is the alphabet which is 3 letters after the input, which is 'd'. Similarly, all the letters are transformed into some other one, and the resulting string does not make sense at all, unless you decrypt it! Here is a graphical representation of what's happening in a caesar shift.



So, 'Hello, World!' gets transformed into, 'Khoor, zruog!', if the shift number is 3.

Now let's see the code, shall we?

Code
#include<iostream.h>
#include<string>
void main()
{
    cout<<"Enter the input message:\n";
    char inmsg[1000];
    int i, j, length,choice;
    cin.getline(inmsg,100);
    length = strlen(inmsg);
    cout<<"Enter your choice \n1. Encrypt \n2. Decrypt \n";
    cin>>choice;
    if(choice==1)
    {
         for(i=0;i<length;i++)
         {
             if(isalpha(inmsg[i]))
             {
                 inmsg[i]=tolower(inmsg[i]);
                 for(j=0;j<3;j++)
                 {
                    if(inmsg[i]=='z')
                    {
                        inmsg[i]='a';
                    }
                    else
                    {
                        inmsg[i]++;
                    }
                 }
             }
         }
         cout<<"The encrypted message is \""<<inmsg<<"\"\n";
    }
    else if (choice == 2)
    {
        for(i=0;i<length;i++)
        {
             if(isalpha(inmsg[i]))
             {
                 inmsg[i]=tolower(inmsg[i]);
                 for(j=0;j<3;j++)
                 {
                     if(inmsg[i]=='a')
                     {
                        inmsg[i]='z';
                     }
                     else
                     {
                        inmsg[i]--;
                     }
                 }
            }
       }
    cout<<"The decrypted message is \""<<inmsg<<"\"\n";
    }
}

For newer IDEs, visit this page.

Working

NOTE: The header 'string.h' is used in the program, which includes functions like strlen(), isalpha(), tolower()

First, we input the character string from the user. Also, we ask whether he wants to encrypt or decrypt the message given.
If he chooses to encrypt : We loop through the string array, and check whether all the characters are alphabets using the function, 'isalpha()', and also we change all the given upper case characters to lower case using 'tolower()'. Now we loop through it again, but this time only by the number of times to shift the alphabets, which in this case is 3. Now we encounter a problem : What if the user enters the letters 'x','y', or 'z'? There is no letter beyond 'z' right? This can be solved by a little adjustment. If the loop encounters the letter 'z', it's gonna automatically change it to 'a' on the next run. In all other cases, we just increment the letter by 1. So even if the user gives 'x', the loop increments the letter by 1 until it reaches 'z', and it's gonna change it to 'a'. Haha! That's all with encryption! Here is an output, when the user chooses encryption


Notice how the program is retaining those symbols without any change? This is because we put the function isalpha(). This function checks whether if a character is alphabet or not, and if it is not, the loop does not touches that character.
If he chooses to decrypt : Now also everything is same as above, except that the problem changes to the letter 'a', and the adjustment is made the other way around. Also, the letter is decremented by 1 instead of being decremented. Here is a sample decryption output.




That's all for today, guys...
kdssb frglqj!*


*Try on your decryptor! ;-)


Friday, 8 November 2013

Number of characters in a string - C++ Program Source Code!

This program is to count the number of spaces in an input string.

Here is the code:
#include<iostream.h>
#include<conio.h>

void main()
{

int spaces, i;
char str[100];
int spaces = 0;

cout<<"Enter the string!"<<endl;
cin.getline(str, 100);
cout<<"Entered string is \'"<<str<<"\'"<<endl;

for(i=0;i<=100;i++)
{
 if (str[i] == ' ')
 {
  spaces = spaces + 1;
 }
}
cout<<"There are "<<spaces<<" spaces in the given string!"<<endl;
}

For newer IDEs (like Dev C++):
#include<iostream>
using namespace std;
void main()
{
   int spaces, i;
   char str[100];
   int spaces = 0;
   cout<<"Enter the string!"<<endl;
   cin.getline(str, 100);
   cout<<"Entered string is \'"<<str<<"\'"<<endl;
   for(i=0;i<=100;i++)
   {
      if (str[i] == ' ')
      {
         spaces = spaces + 1;
      }
   }
   cout<<"There are "<<spaces<<" spaces in the given string!"<<endl;
   return 0;
}


Working:
Here we use the concept 'String, basically is an array!'. First we input the string str. Let spaces be the number of spaces in the string. Initially it is set to 0. We are going to loop through the string (array), and if any of the element in that array is found to be ' ', or a space, spaces is incremented by 1. By the end, the spaces is going to be the total number of spaces in the string. 

You might also like