Friday, November 11, 2011

string program in C

/*
 Write a program that accepts two strings.
 * the program checks whether the second word is present anywhere in any word.
 * it the word is found then it prints the word from second string.
 * 
 * An example could be:
 * Input : 
 * Entr first string: this is a iss thiss. al a/
 * Enter second string: is
 * output: this is iss thiss.
 */


/* 
 * File:   main.c
 * Author: umang
 *
 * Created on November 11, 2011, 3:08 PM
 */

#include <stdio.h>
#include <stdlib.h>

#include<string.h>
#define true 0
#define false 1

//int main(int argc, char** argv)

void main()
{

    char arr[100] = {'\0'}; // initialize whole array


    char word[10] = {'\0'};

    int i, j, temp;

    int b = false;
    printf("Enter string: ");
    gets(arr);

    printf("Enter word: ");
    gets(word);
    for (i = 0; arr[i] != '\0'; i++)
    {

        temp = i;
        b = false;
        for (j = 0; word[j] != NULL && arr[j + i] != NULL; j++)
        {

            if (word[j] != arr[i + j])
            {

                b = true;
            }
        }

        if (b == false)
        {

            j--;
            if (arr[i + j] != ' ')
            {

                while (arr[i + j] != ' ' && arr[i + j] != NULL)
                {

                    i++;
                }
            }

            while (arr[temp] != ' ' && temp >= 0)
            {

                temp--;
                if (temp < 0)
                {
                    temp = 0;

                    break;
                }
            }
            if (temp != 0)
            {
                temp++;
            }

            for (; arr[temp] != ' ' && arr[temp] != NULL; temp++)
            {

                printf("%c", arr[temp]);
            }

            printf(" ");

            i = temp;
        }
    }
    //return (EXIT_SUCCESS);
}
// input
// this a te is thisi is. t
//     4       12   18   22 

No comments:

Post a Comment