for Robot Artificial Inteligence

11. strings

|

  1. Character set/ASCII codes
  2. character array
  3. string
  4. creating a string

Length of a string

int main()
{
  char * s = "welcome";
  int i;
  for(i=0; s[i]!='\n';i++)
  {
  }
  printf(" length is %d ", i);
  return 0;
}

changing case

int main()
{
  char A[] = "Welcome";
  int i ;
  for(i=0; A[i]!='\n',i++)
  {
    if(A[i]>=65 && A[i]<=90)
      A[i]=A[i]+32;
    else if(A[i]>='a'&&A[i]<=122)
      A[i]-=32;
  }

}

Counting Words

int main()
{
  char A[]="how are you";
  int i, word=1;
  for(i=0;A[i]!='\n';i++)
  {
    if(A[i]==' ')
      word ++;
  }
  printf("%d", word);
}

Counting words 2

int main()
{
  char A[]="how are you";
  int i, word=1;
  for(i=0;A[i]!='\n';i++)
  {
    if(A[i]==' ' && A[i-1]==' ')
      word ++;
  }
  printf("%d", word);
}

Counting Capital letter and small letter

Validate a string

int validate(char * name)
{
  int i;
  for(i=0; name[i]!='\n'; i++)
  {
    if(!(name[i]>=65 && name[i]=<90) && !(name[i]>=97 && name[i]<=122) && !(name[i]>=48 && name[i]<=59))
    {
      return 0;
    }
  }
  return 1;
}
int main()
{
  char * name="Ani?321";
  if(validate(name))
  {
    printf("valid string");
  }
  else
  {
    printf("no valid string");
  }
  return 0;
}

Reversing a strings

int main()
{
  char A[]="python"
  char B[7];
  int i;
  for(i=0; A[i]!='\n';i++)
  {
    i = i-1;
  }
  for(int j=0; i>=0; i--, j++)
  {
    B[j]=A[i];
  }
  B[j]='\n'
  printf("%s\n",B );
}

method 2

int main()
{
  char A[] = "python";
  char B[7], t;
  int i,j;
  for(j=0;A[j]!='\n';j++)
    j=j-1
  for(i=0;i<j;i++;j--;)
  {
    t = A[i];
    A[i]=A[j];
    A[j]=t;
  }
}

Comparing string & Palindrome

Example 1

int main()
{
  char A[]="painter";
  char B[]="painting";
  int i,j;
  for(i=0, j=0;A[i]!='\n'&&B[j]!='\n';j++,i++)
  {
    if(A[i]!=B[j])
      break;
    if(A[i]==B[j])
      printf("equal");
    else if(A[i]<B[j])
      printf("small");
    else
      printf("greater");
  }

}

Finding Duplicate in a string

  • compare with other letters
  • using Hash table as counting
  • using bits

int main()
{
  char A[]="finding";
  int H[26], i;
  for (i=0;A[i]!='\n';i++)
  {
    H[A[i]-97]+=1;
  }
  for(i=0;i<26;i++)
  {
    if(H[i]>1)
      printf("Dupicate", A[i]);
  }
}

Method2

using bitwise operation

  1. left swift
  2. bits Oring(Merging)
  3. bits ANding(Masking)

int main()
{
  char A[]="finding";
  long int H=0, x=0;
  for(int i =0; A[i]!='\n';i++)
  {
    x=1;
    x=x<<A[i]-97'
    if(x&H>0)
    {
      printf("Duplicate")
    }
    else
      H=x:H;
  }
}

Check for Anagram

int main()
{
  char A[]="decimal"
  char B[]="Medical"
  int i, H[26]={0};
  for(i=0;A[i]!='\n';i++)
  {
    H[A[i]-97]+=1;
  }
  for(i=0;B[i]!='\n';i++)
  {
    H[B[i]-97]-=1;
    if(H[A[i]-97]<0)
    {
      printf("not anagram");
      break;
    }
  }
  if(B[i]=='\n')
    printf("its anagram");
}

permutation of string

void perm(char s[], int k)
{
  static int A[10] = {0};
  static char Res[10];
  int i;
  if(s[k]=='\n')
  {
    Res[k]='\n'
    printf("%s\n", );  
  }
  else
  {
    for(i=0;s[i]!="\n";i++)
    {
      if(A[i]==0)
      {
        Res[k]=s[i];
        A[i]=1;
        perm(s,k+1);
        A[i]=0;
      }
    }    
  }
}
int main()
{
  char s[]="ABC"''
  perm(s,0);
  return 0;
}

Method 2

void perm(char s[], int l, int h)
{
  int i;
  if(l==h)
  {
    printf("%s\n",s);
  }
  else
  {
      for(i=l,l<=h;i++)
      {
        swap(s[l],s[i]);
        perm(s,l+1,h);
        swap(s[l],s[i]);
      }    
  }

}
int main()
{
  char s[]="ABC"
  int h = sizeof(s)/sizeof(s[0]);
  perm(s,0,h);
  return 0;
}

Comments