Tuesday, October 28, 2014

C String program



Reverse every Word of given String

#include 
#include 
#include 
void main()
{
    int i, j = 0, k = 0, x, len;
    char str[100], str1[10][20], temp;
    clrscr();
    printf("enter the string :");
    scanf("%[^\n]s", str);
 
    for (i = 0;str[i] != '\0'; i++)
    {
        if (str[i] == ' ')
        {
            str1[k][j]='\0';
            k++;
            j=0;
        }
        else
        {
            str1[k][j]=str[i];
            j++;
        }
    }
    str1[k][j] = '\0';
 
    for (i = 0;i <= k;i++)
    {
        len = strlen(str1[i]);
        for (j = 0, x = len - 1;j < x;j++,x--)
        {
            temp = str1[i][j];
            str1[i][j] = str1[i][x];
            str1[i][x] = temp;
        }
    }
    for (i = 0;i <= k;i++)
    {
        printf("%s ", str1[i]);
    }
}
 
Output
 
Enter the string :C Programming Class
C gnimmargorP ssalC

List All Lines containing a given String

#include 
#include 
#include 
#include 
int search(FILE *, char *);
void main(int argc, char * argv[])
{
    FILE *fp1;
    int p;
 
    fp1 = fopen(argv[1], "r+");
    if (fp1 == NULL)
    {
        printf("cannot open the file ");
        exit(0);
    }
    search(fp1, argv[2]);
    fclose(fp1);
}
 int search(FILE *fp, char * str)
{
    FILE *fp1;
    fp1 = fopen("fp1","w");
    char s[10],c;
    int len = strlen(str);
    int i = 0;
    int d;
    int seek = fseek(fp, 0, 0);
    c = fgetc(fp);
    while (c != EOF)
    {
        if (c == ' ' || c == '\n')
        {
            s[i] = '\0';
            i = 0;
            if (strcmp(s, str) == 0)
            {
                while (c = fgetc(fp) != '\n')
                {
                    fseek(fp, -2L, 1);
                    d = ftell(fp);
                }
                while ((c = fgetc(fp)) != '\n')
                {
                    fputc(c, fp1);
                }
            }
        }
        else
        {
            s[i] = c;
            i++;
        }
        c = fgetc(fp);
    }
    return 1;
    getch();
}
 
Output
 
hi hello everyone
again hi to the late comers
welcome to the class
 
hi hello everyone
again hi to the late comers

Concatenate two Strings Lexically

#include 
#include 
#include 
void sort(char *p);
void main()
{
    char string1[100], string2[100];
    int i, len, j;
    printf("\nEnter a string : ");
    scanf("%[^\n]s", string1);
    printf("\nEnter another string to concat : ");
    scanf(" %[^\n]s", string2);
    len = strlen(string1);
    string1[len] = ' ';
    for(i = 0, j = len + 1; i < strlen(string2); i++, j++)
        string1[j] = string2[i];
    string1[j]='\0';
    sort(string1);
}
 void sort(char *p)
{
    char temp[100];
    char a[100][100];
int t1, i, j = 0, k = 0, l = strlen(p), x = 0, y = 0, z = 0, count, l1, l2;
 
    for (i = 0; i < l; i++)
    {
        if (p[i] != ' ') 
        {
            a[k][j++] = p[i];
        }
        else
        {
            a[k][j] = '\0';
            k++;
            j = 0;
        }
    }
 
    t1 = k;
    k = 0;
    for (i = 0; i < t1; i++)
    {
        for (j = i + 1; j <= t1; j++)
        {
            l1 = strlen(a[i]);
            l2 = strlen(a[j]);
            if (l1 > l2)
                count = l1;
            else
                count = l2;
            x = 0, y = 0;
            while ((x < count) || (y < count))
            {
                if (a[i][x] == a[j][y])
                {
                    x++;
                    y++;
                    continue;
                }
                else 
                    if (a[i][x] < a[j][y]) break;
                else 
                    if (a[i][x] > a[j][y])
                    {
                        for (z = 0; z < l2; z++)
                        {
                            temp[z] = a[j][z];
                            a[j][z] = '\0';
                        }
                        temp[z] = '\0';
 
                        for (z = 0; z < l1; z++)
                        {
                            a[j][z] = a[i][z];
                            a[i][z] = '\0';
                        }
                        a[j][z] = '\0';
 
                        for (z = 0; z < strlen(temp); z++)
                        {
                            a[i][z] = temp[z];
                        }
                        break;
                    }    
                }
            }    
        }
    for (i = 0; i < l; i++)
    p[i] = '\0';
    k = 0;
    j = 0;
    for (i = 0; i < l; i++)
    {
        if (a[k][j] != '\0')
        {
            p[i] = a[k][j++];
        }
        else
        {
            k++;
            j = 0;
            p[i] = ' ';
        }
    }
    puts(p);       
    getch(); 
}
 
 Output
 
Enter a string : hello this
Enter another string to concat : is sanfoundry
hello is sanfoundry this

No comments:

Post a Comment