QUESTION-28 Find total number of alphabets, digits or special characters in a string

Answer-
#define MAX_SIZE 100 // Maximum string size
int main()
{
    char str[MAX_SIZE];
    int alphabets, digits, others, i;

    alphabets = digits = others = i = 0;

    printf("Enter any string : ");
    gets(str);

    while(str[i]!='\0')
    {
        if((str[i]>='a' && str[i]<='z') || (str[i]>='A' && str[i]<='Z')) { alphabets++; } else if(str[i]>='0' && str[i]<='9')
        {
            digits++;
        }
        else
        {
            others++;
        }

        i++;
    }
    printf("Alphabets = %d\n", alphabets);
    printf("Digits = %d\n", digits);
    printf("Special characters = %d", others);

    return 0;
}

 

QUESTION-29 Find the first occurrence of a character in a string

Answer-
#define MAX_SIZE 100 // Maximum string size

int indexOf(const char * str, const char toFind);


int main()
{
    char str[MAX_SIZE];
    char toFind;
    int index;

    printf("Enter any string: ");
    gets(str);
    printf("Enter character to be searched: ");
    toFind = getchar();

    index = indexOf(str, toFind);

    if(index == -1)
        printf("'%c' not found.", toFind);
    else
        printf("'%c' is found at index %d.", toFind, index);

    return 0;
}

int indexOf(const char * str, const char toFind)
{
    int i = 0;

    while(str[i] != '\0')
    {
        if(str[i] == toFind)
            return i;
        i++;
    }

    return -1;
}

 

QUESTION-30 Find last occurrence of a character in a string

Answer-

#define MAX_SIZE 100 // Maximum string size

int lastIndexOf(const char * str, const char toFind);


int main()
{
    char str[MAX_SIZE];
    char toFind;
    int index;

    printf("Enter any string: ");
    gets(str);

    printf("Enter any character to find: ");
    toFind = getchar();

    index = lastIndexOf(str, toFind);

    printf("\nLast index of '%c' is %d", toFind, index);

    return 0;
}

int lastIndexOf(const char * str, const char toFind)
{
    int index = -1;
    int i = 0;

    while(str[i] != '\0')
    {
        if(str[i] == toFind)
        {
            index = i;
        }
        i++;
    }

    return index;
}

 

QUESTION-31 Search all occurrences of a character in a string

Answer-
#define MAX_SIZE 100 // Maximum string size 

int main()
{
    char str[MAX_SIZE];
    char toSearch;
    int i;

    printf("Enter any string: ");
    gets(str);
    printf("Enter any character to search: ");
    toSearch = getchar();

 
    i=0;
    while(str[i]!='\0')
    {
        /* If character is found in string */
        if(str[i] == toSearch)
        {
            printf("'%c' is found at index %d\n", toSearch, i);
        }

        i++;
    }

    return 0;
}

 

QUESTION-32 Compare Strings

Answer-
int main() {
   char s1[] = "advise";     
   char s2[] = "advice";
    
   int n = 0;
   unsigned short flag = 1; 
    
   while (s1[n] != '\0') {
      if(s1[n] != s2[n]) {
         flag = 0;
         break;
      }
      n++;
   }
    
   if(flag == 1) {
      printf("%s and %s are identical\n", s1, s2);
   }else {
      printf("%s and %s are NOT identical\n", s1, s2);
   }
   return 0;
}

 

QUESTION-33 Reverse a line

Answer-
int string_length(char s[]) {
   int i=0;

   while(s[i]!='\0')
      i++;

   return i;	
}

void string_reverse(char st[]) {
   int i,j,len;
   char ch;

   j = len = string_length(st) - 1;
   i = 0;

   while(i < j) { ch = st[j]; st[j] = st[i]; st[i] = ch; i++; j--; } } int main (void) { char line[] = "Taj Mahal is one of the seven wonders of the world"; char reverse[100] = "", temp[50]; int i, j, n; n = string_length(line); for(i = n-1; i >= 0; --i) {

      for(j = 0; i >= 0 && line[i] != ' '; --i,++j)
         temp[j] = line[i];

      temp[j] = '\0';

      string_reverse(temp);
      
      strcat(reverse,temp);
      strcat(reverse," ");
   }

   printf("Original - %s\n", line);
   printf("Reversed - %s\n",reverse);
   return 0;
}

 

 [/vc_column_text][/vc_column][/vc_row]