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

Saturday, August 20, 2011

First non repeating character in a string

Problem taken from http://programmingpraxis.com/
=================================================
Write a function that takes an input string and returns the first character from the string that is not repeated later in the string. For instance, the two letters “d” and “f” in the input string “aabcbcdeef” are non-repeating, and the function should return “d” since “f” appears later in the string. The function should return some indication if there are no non-repeating characters in the string.
=================================================

Here is my solution ::
-- I am going over the length of the string, breaking whenever I find a non-repeating char.
For ease of searching a char, I am copying the entire string, expect the "char" I am currently looking at to a new string and search for that char in the new appended string.
If found implies it's repeated, else non-repeating


<pre class="prettyprint">
int main () {

    char str[80];
    char str_a[80];
    int len, i;
    char c;

    printf("Please enter the string to find non-repeating char\n");
    scanf("%s",str); 
    len = strlen(str);

    for (i = 0; i < len ; i++) {
        c = str[i];
       //copy rest of the string except char 'c' into another
        memcpy(&str_a[0] , &str[0], i); 
        memcpy(&str_a[i] , &str[i+1], (len-i-1));
        if (!strchr(str_a, c)) {
            printf("First non repeating char in string : \"%c\"\n",c);
            break;
        }   
    }   
    if (i == len)
        printf("Non repeating char not found in given string\n");
}

Friday, August 19, 2011

String reversal with words in tact!!

I am on a freaking C programming spirit for past two days!! I feel like coding all the way!!

Problem statement
===============================================

Reverse a string with words, with words intact. 

Example will make it explain better 

Input string:: "I am a programming freak!"
Output :: "freak! programming a am I" 



Another -- "I use Mac book" i.e. "book Mac use I"
===============================================
Here is my working Solution!!
===============================================

<pre class="prettyprint">
int main()
{
    char *orig_str = NULL;
    char *rev_str;
    char str[80]; //This is a limitation right now
    char delims[] = " ";
    int orig_len,len =0,slen =0;

    printf("Please enter the string you want to reverse in words\n");
    fgets(str, 80, stdin); //fgets has ugly trailing '\n' too
    len = strlen(str);
    
    if (str[len-1] == '\n')
        str[len-1] = '\0';

    orig_len = len = strlen(str); //new str len with out \n

    rev_str = (char *)malloc(len+1);
    rev_str[len] = '\0';

    orig_str = strtok(str, delims);
    while (orig_str != NULL) {
            slen = strlen(orig_str);
    
            //this to prevent adding space for the first word
            //in orig_str which will be the last in rev_str
            if (len < orig_len)
               strncpy(&rev_str[len]," ",1);

            len = len - slen;
            strncpy(&rev_str[len],orig_str,slen);

            len--;  // for space added to the rev_str

            orig_str = strtok(NULL,delims);
    }
    printf("\nString reversed in words is :\"%s\"\n",rev_str);
    free(rev_str);
}