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

Monday, 3 October 2016

A Program to check if strings are rotations of each other or not

A Program to check if strings are rotations of each other or not

String-Rotations
String-Rotations



#include <iostream>
#include <cstring>
#include<string>
using namespace std;
void CompareString(string, string, int);
int ComputeLength(string str);
int main()
{
string str = ""; string str1 = ""; int len = 0, len1 = 0;
cout << "\nenter string ";
cin >> str;
cout << "\nenter string 2 to compare:-  ";
cin >> str1;

len = ComputeStringLength(str);
len1 = ComputeStringLength(str1);
if (len == len1)
CompareString(str, str1, len);
else
cout << "rotation not possible";
getchar();
return 0;
}

int ComputeStringLength(string str)
{
int len = 0;
for (int i = 0; str[i] != '\0'; i++)
{
len++;
}
return len;
}


void  CompareString(string str, string str1, int n)
{
int index = 0, flag = 0, curr_index = 0, count1 = 0, flagj = 0;
for (int i = 0; i<n; i++)
{
for (int j = flagj; j<n; j++)
{
if (str[i] == str1[j])
{
index = j;
flagj =j;
count1++;
flag++;
if (flag == 1)
{
curr_index = index;
}
break;
}

}
}
int temp = count1;
if (count1 != n)
{
if (curr_index>=0)
{
int k = 0;
for (int i = n - 1; i>n - curr_index - 1; i--)
{
if (str[i] == str1[k])
{
temp++;
k++;
}

}
}
if (temp == n)
{
cout << "\n\nstring is same after rotation";
}
else
{
cout << "\n\nstring is not same after rotation";
}
}
else
{
cout << "\n\nstring is same after rotation";
}

}



Sunday, 2 October 2016

Calculate Sum Of All Numbers Present In String




Calculate sum of all numbers present in a string

sum-of-all-digits-present-in-a-string
sum-of-all-digits-present-in-a-string

#include <iostream>
#include<stdio.h>
#include<string.h>
using namespace std;

int SumOfDigits(string str, int n);
int SumOfoverallNumberPresent_In_Sequence(string str, int n);

int main()
{
   char str[20];

   printf("Please enter the string:");
   gets(str);
   int length = strlen(str);
   cout<<"\n\nsum of all digits present in string is :-  "<<SumOfDigits(str, length)<<endl;
   cout<<"\n\nSum of different numbers present in string:-  "<<SumOfoverallNumberPresent_In_Sequence(str, length)<<endl;
   return 0;
  }


 int SumOfDigits(string str, int n)
 {
     int i,sum=0,num=0;
     for(i = 0;i<=n;i++)
   {
      if(str[i] >=48 && str[i] <= 57)
      {
         num = str[i]-48;
         sum = sum  + num;
      }

    }
     return sum;
 }

 int SumOfoverallNumberPresent_In_Sequence(string str, int n)
 {
    int i,sum=0,digitSum=0,Flag=0,num=0;
    for(i = 0;i<=n;i++)
    {
      if(str[i] >=48 && str[i] <= 57)
      {
         num = str[i]-48;
         sum = sum*10  + num;
      }
      else
      {
          digitSum = digitSum+sum;
          sum=0;
      }

    }
     return digitSum;
 }

Other Articles:
Kth smallest element , Merge Point In LinkList, Compare two String Represented in Link-List


Article Sources For Learning

Other Blogs: Follow here