Sailing in the sea of C, C++ & c#

Friday, June 02, 2006

String Manipulation Functions

//String Concatenation function

#include "stdafx.h"
#using <mscorlib.dll>
using namespace System;

char* StringCat(char* str1, char* str2)
{
     int len;
     int index;
     int index2 = 0;
     int len2;
     char* str3;
     len = strlen(str1) + strlen(str2);
     str3 =  new char[len + 1];

     if(strlen(str1) == 0)
     {
          return str2;
     }
     if(strlen(str2) == 0)
     {
          return str1;
     }
     for(index = 0; str1[index] != '\0'; index++)
     {
          str3[index] = str1[index];
          
     }
     len2 = strlen(str2);
     for(index2 = strlen(str1); str2[index2 - len2 - 1] != '\0'; index2++)
     {
          str3[index2] = str2[index2 - len2 - 1];
     }
     str3[len] = '\0';
     return str3;

}
//String Comparison Function

#include "stdafx.h"
#using <mscorlib.dll>
using namespace System;

int StrComp(char* str1, char* str2)
{
     int len;
     int flag = 0;
     int index;
     if(strlen(str1) < strlen(str2))
     {
          return -1;
     }
     else if(strlen(str1) > strlen(str2))
     {
          return 1;
     }
     else
     {
          for(index =0; str1[index] != '\0' ; index++)
          {
               if(str1[index] < str2[index])
               {
                    flag =-1;
                    return -1;
                    
               }
               else if(str1[index] > str2[index])
               {
                    flag =1;
                    return 1;
               }
               else
               {
                    flag = 0;
               }
          }
     }
     if(flag == 0)
     {
          return 0;
     }
     
}
int _tmain()
{
     int i;

     i = StrComp("abdc", "abc");
     switch(i)
     {
     case -1:
          printf("\nThe first string is smaller \n");
          break;
     case 0:
          printf("\nThey are equal \n");
          break;
     case 1:
          printf("\nThe first string is greater \n");
          break;
     default:
          break;
     }
     return 0;
}




//Find a substring in a given string- Returns the index of the substring in the main string

#include "stdafx.h"
#using <mscorlib.dll>
using namespace System;

int SubString(char* passedString, char* subString)
{
     int index =0;
     int subIndex = 0;
     int flag = 0;
     while((passedString[index] != '\0') && (subString[subIndex] != '\0'))
     {
          if(passedString[index] != subString[subIndex])
          {
               index++;
               flag = 0;
          }
          else
          {
               index++;
               subIndex++;
               flag =1;
          }
     }
     if(flag == 1)
     {
          return (index - subIndex);
     }
     else
     {
          return -1;
     }
}

int _tmain()
{
        int result;

     result = SubString("I like a dog", "like");
     if(result >= 0)
     {
          printf("\n the sub string is found at index %d \n", result);
     }
     else
     {
          printf("\n the substring is not found\n");
     }
     return 0;
}



//Find if a word is a Palindrome

#include "stdafx.h"
#using <mscorlib.dll>
using namespace System;

bool Palyndrome(char* string)
{
     int index = 0;
     int length;
     int flag = 0;
     length = strlen(string);
     char c;

     //Convert to UPPER by subtracting 32 if lower case found
     while(string[index] != '\0')
     {
          if((string[index] >= 'a') && (string[index] <= 'z'))
          {
               c = string[index] + 'A' - 'a' ;
               string[index] = c;
          }
          index++;
     }
     printf("\nconverted string %s", string);
     index = 0;
        while(string[index] != '\0')
     {
          if(string[index] == string[length - index - 1])
          {
               flag = 0;
          }
          else
          {
               flag = 1;
          }
          index++;     
     }
     if(flag == 0)
     {
          return true;
     }
     else
     {
          return false;
     }
}

int _tmain()
{
        char* str;
     bool result;
     result = Palyndrome("Madam");
     if(result)
     {
          printf("\nThe string is a palindrome\n");
     }
     else
     {
          printf("\nThe string is not a palindrom\n");
     }
     return 0;
}

Sailing in the sea of C, C & c#

Sailing in the sea of C, C & c#