About Me

My photo
Mostly software programming related blogs.
Showing posts with label StringProgrammingQuestions. Show all posts
Showing posts with label StringProgrammingQuestions. Show all posts

Thursday, April 30, 2015

strtok in C/C++ Program - Code implementation - string tokenizer

#include<iostream>
#include <inttypes.h>
#include <string.h>
#include <stdio.h>

using namespace std;

char* my_strtok(char *inp, char *delim);

main()
{
        char src[128] = {'\0'};
        char delim[16] = {'\0'};

        char *out;

        cout << "enter input string src: ";
        gets(src);
        cout << "enter input string dilimiter: ";
        gets(delim);

        cout << "src is " << src << endl;
        cout << "delimiter is " << delim << endl;

        out  = my_strtok(src, delim);  

        while(out != NULL) {
                cout << "token is " << out << endl;
                out = my_strtok(NULL, delim);
        }  
}

char *my_strtok(char *inp, char *deli) {

        static char *start = NULL;
        char *ch = NULL, *delim = NULL, *ret = NULL;
        uint32_t num_chars = 0;

        if (inp != NULL) {
                start = inp;
        }  

        if (start == NULL || deli == NULL) {
                return NULL;
        }  

        ch = ret = start;

        while (*ch != '\0') {
                delim = deli;
                while(*delim != '\0') {
                        if (*ch == *delim) {
                                *ch = '\0';
                                if (num_chars > 0) {
                                        start = ch+1;
                                        return ret;
                                } else {
                                        ret = ch+1;
                                        break;
                                }
                        }
                        delim++;
                }
                ch++;
                num_chars++;
        }
        if (*ch == '\0') {
                start = NULL;
                if (inp == NULL && strlen(ret) != 0) {
                        return ret;
                }
        }
        return NULL;
}

Wednesday, September 10, 2014

Given an integer find the immediate larger integer which is a palindrome

// #palindrome #C #Programming #String 

#include <stdio.h>
#include <string.h>

typedef unsigned int uint;

uint find_next_palin(uint inp);

main()
{
        uint num;
        printf("enter the number: ");
        scanf("%u", &num);

        uint ret = find_next_palin(num);

        printf("next palindrome is %u\n", ret);
}

uint find_next_palin(uint inp)
{
        char buff[32];
        uint ret, i1 =0, i2 =0, i = 0;

        sprintf(buff, "%d", inp);

        int len = strlen(buff);

        if (len == 1) {
                return inp;
        }

        if (len % 2 == 0) {
                i1 = buff[len/2 -1] - '0';
                i2 = buff[len/2] - '0';

                if (i1 < i2) {
                        buff[len/2 -1] = i1 + '0'+1;
                }
                for (i = 0; i < len/2; i++) {
                        buff[len/2+i] = buff[len/2-i-1];
                }
        } else {
                i1 = buff[len/2 -1] - '0';
                i2 = buff[len/2 +1] - '0';

                if (i1 < i2) {
                        buff[len/2 -1] = i1 + '0'+1;
                }
                for (i = 0; i < len/2; i++) {
                        buff[len/2+i+1] = buff[len/2-i-1];
                }
        }

        sscanf(buff, "%u", &ret);
        return ret;
}                                                                                                                                                                                  

Friday, October 26, 2012

Length of the longest substring without repeating characters

length of the longest substring without repeating characters in a given string

Reverse a string



C/C++ solution:

using namespace std;

#include<iostream>
#include<string.h>

string reverse(string input);

int main() {
        string input;
        cout << "Enter the string : " ;
        getline(cin, input);
        string output = reverse(input);
        cout << "Revered string :" << output << "\n";
}

string reverse(string input) {
        int len = input.length();
        int i = 0;
        char tmp;
        while(i < len/2) {
                tmp = input[i];
                input[i] = input[len -i -1];
                input[len - i - 1] = tmp;
                i++;
        }  
        return input;
}

Replace blanks with a string in a given string


Problem - Given a string, replace all blanks with "%20"

C/C++ solution


#include <iostream>
#include <string.h>

using namespace std;

main(int argc, char* argv[]) {

        string input = "i am boy. You are Mrs Smith. ";

        int i  = 0;

        int len = input.size();

        cout << "input len before replace = " << len << endl;

        while (i < len) {
                if ( input[i] == ' ') {
                        input.replace(i, 1, "%20");
                        len = len + 2;
   
                }  

                i++;
        }  

        cout << "input = " << input << endl;

        cout << "input len after replace = " << input.size() << endl;
}

 ./a.out 
input len before replace = 29
input = i%20am%20boy.%20You%20are%20Mrs%20Smith.%20
input len after replace = 43

String rotation


/* Program to check given two strings s1 and s2, if one is rotation of
 * other one. For example student is rotation of dentstu.
 */
#include <iostream>
#include <string.h>

using namespace std;

main() {
        string s1, s2;
   
        cout << "Enter both the strings ";

        cin >> s1;
        cin >> s2;

        string temp = s1+s1;

        if (temp.find(s2) == string::npos) {
                cout << s1 << " and " << s2 << " are not rotation of each other." << endl;
        } else {
                cout << s1 << " and " << s2 << " are rotation of each other." << endl;
        }  

}

$ ./a.out 
Enter both the strings student dentstu
student and dentstu are rotation of each other.

$ ./a.out 
Enter both the strings abc acb
abc and acb are not rotation of each other.

Thursday, October 25, 2012

Check if unique characters in a string


C/C++ solution:

#include <string.h>
#include <iostream>

using namespace std;

main(int argc, char* argv[]) {

        string input ;
        cout << "Input string: ";
        cin >> input;

        bool val[26]; // assuming only english alphabets
        fill_n(val, 26, false);//{ [0 ... 25] = false };

        int len = input.size();

        if (len > 26) {
                cout << "string does not have unique characters." << "\n";
                return 0;
        }  
        while (len >= 0) {
                if(val[tolower(input[len-1]) - 'a'] == true) {
                        cout << "string does not have unique characters." << "\n";
                        break;
                } else {
                        val[tolower(input[len-1]) - 'a'] = true;
                }  
                len--;
           
        }  
        if (len < 0) {
                        cout << "string has all unique characters." << "\n";
        }  
}

Compress String


/* compress the string for example aabbbcccccdeff should become a2b3c5d1e1f2
 * if compress string length remain same as original string then return
 * original string */

using namespace std;

#include <iostream>
#include <string.h>
#include <stdio.h>

main() {

        string input;
        cout << " enter input string to be compressed: ";
        cin >> input;

        string::size_type len = input.size();

        string::size_type orig_len = len;

        string::size_type i = 0;

        int sameCharCount = 1;

        bool in_progress = false;

        char buf[10];

        while (i < len) {
   
                if (input[i] == input[i+1]) {
                        in_progress = true;
                        sameCharCount++;
                } else if (in_progress) {
                        in_progress = false;
                        sprintf(buf, "%d", sameCharCount);
                        input.replace(i-sameCharCount+2, sameCharCount-1, buf);
                        len = len - sameCharCount + 1 + strlen(buf);
                        i = i - sameCharCount + 1 + strlen(buf);
                        sameCharCount = 1;

                } else {
                        input.insert(i+1, "1");
                        i++;
                        len++;
                }
                i = i + 1;
        }

        if (input.size() < orig_len) {
                cout << "changed string: " << input << endl;
        } else {
                cout << "string was not compressed" << endl;
        }
}
$ ./a.out 
 enter input string to be compressed: aaabb
changed string: a3b2
$ ./a.out 
 enter input string to be compressed: ab
string was not compressed
$ ./a.out 
 enter input string to be compressed: acc
string was not compressed
$ ./a.out 
 enter input string to be compressed: b
string was not compressed
$ ./a.out 
 enter input string to be compressed: bc
string was not compressed
$ ./a.out 
 enter input string to be compressed: abc
string was not compressed
$ ./a.out 
 enter input string to be compressed: aaabbbbbbbbbbbbddddddddddd
changed string: a3b12d11