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

Friday, December 19, 2014

Lexicographic sorting of strings

/* Program to perform lexicographic sorting of strings */


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

void main()
{
  char str[50][50], temp[50], str1[50][50], n[1], temp1[50];
  int i, j, m, k;
  printf("\n##############################\n");
  printf("Enter the number of strings\n");
  gets(n);
  m = atoi(n);
  printf("\n##############################\n");
  printf("Enter the strings\n");
  for(i = 0; i < m; i++)
    gets(str[i]);

  // Convert all letters to lowercase
  for(i = 0; i < m; i++)
    for(j = 0; j < strlen(str[i]); j++)
      if(isupper(str[i][j]) != 0)
        str1[i][j] = tolower(str[i][j]);
      else
        str1[i][j] = str[i][j];

  for(i = 0; i < m; i++)
    for(j = i + 1; j < m; j++)
      if((strcmp(str1[i], str1[j])) > 0)
      {
        strcpy(temp, str[i]);strcpy(temp1, str1[i]);
        strcpy(str[i], str[j]);strcpy(str1[i], str1[j]);
        strcpy(str[j], temp);strcpy(str1[j], temp1);
      }

  printf("\n##############################\n");
  printf("Sorted list is \n");
  for(i = 0; i < m; i++)
    puts(str[i]);
  printf("##############################\n");
}


Written by

Monday, December 1, 2014

String replacement program in C

A program to find a particular pattern in a string and replace all occurrences of that pattern with a new pattern to produce a modified string.


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

void main()
{
  char str[100], pattern[50], replace[50], new_str[100], temp[50];
  int l1, l2, l3, flag_eos = 0, pattern_found = 0;
  int i = 0, j, k, n;
  printf("\n###################################\n");
  printf("Enter any string\n");
  gets(str);
  printf("\n###################################\n");
  printf("Enter pattern to be replaced\n");
  gets(pattern);
  printf("\n###################################\n");
  printf("Enter new pattern\n");
  gets(replace);

  l1 = strlen(str);
  l2 = strlen(pattern);
  l3 = strlen(replace);
 
  while(flag_eos == 0)
  {
    for(; i < l1; i++)
      // if any character of str = first character of the pattern
      if((str[i] == pattern[0]) && (i + l2 - 1 < l1))
      {
        for(j = i, k = 0; j < i + l2; j++)
          temp[k++] = str[j];
        temp[k] = '\0';
        break;
      }
    if(i == l1)
    {
      flag_eos = 1;
      temp[0] = '\0';
    }
    if(strcmp(temp, pattern) == 0)
    {
      pattern_found = 1;
      n = 0;
      for(j = 0; j < i; j++)
        new_str[n++] = str[j];
      for(j = 0; j < l3; j++)
        new_str[n++] = replace[j];
      for(j = i + l2; j < l1; j++)
        new_str[n++] = str[j];
      new_str[n] = '\0';
      // Recompute the new string length and
      // continue to look for more occurrences
      strcpy(str, new_str);
      l1 = strlen(str);
    }
    else
      i = i + 1;
  }
  if(pattern_found == 1)
  {
    printf("\n###################################\n");
    printf("The modified string is \n");
    puts(str);
    printf("###################################\n");
  }
  else
  {
    printf("\n###################################\n");
    printf("Pattern not found in the string !!!\n");
    printf("###################################\n");
  }
}

Written by

Saturday, July 21, 2012

Interview question (coding) : Remove duplicates from a string


Problem : Given a string, remove the duplicate characters.
example: BANANAS -> BANS
         bananas -> bans
         BaNanAs -> BaNnAs
         1232416 - > 12346
         
#include<stdio.h>
 
int main()
{
  char s1[10], s2[10];
  int i, j, c;
  s2[0] = '\0';
 
  printf("Enter the string : ");
  scanf("%s",s1);
 
  for(i = 0; s1[i] != '\0'; i++)
  {
    c = 0;
    for(j = 0; (s2[j] != '\0' && c == 0); j++)
    {
      if(s1[i] != s2[j])
        continue;
      else
        c = 1;
    } 
    if(c == 0)
    {
      s2[j++] = s1[i];
      s2[j] = '\0';
    }
  }
  printf("\nString after removing duplicates:%s\n", s2);

  return 0;
}

Written by

Friday, May 13, 2011

Check if a String is Palindrome in C

"A palindrome is a word, phrase, number, or other sequence of units that may be read the same way in either direction, with general allowances for adjustments to punctuation and word dividers." - Wikipedia

This is a C program to check if a given string is a palindrome or not.
Written by