top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to remove all characters from an alphanumeric string using C?

+3 votes
547 views
How to remove all characters from an alphanumeric string using C?
posted Feb 4, 2016 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote
 
Best answer
#include <stdio.h>

char strip_off_chars(char *str)
{
   int cnt = 0;
   char *ptr = str;

   while(*ptr)
   {
      if(*ptr >= '0' && *ptr <= '9')
      {
         cnt++;
         str[cnt - 1] = *ptr;
      }
      ptr++;
   }

   str[cnt] = '\0';
   return;
}

int main()
{
   char str1[] = "Jagadish1234";
   char str2[] = "1234Jagadish";
   char str3[] = "jaga1234dish";
   char str4[] = "ja1ga2di3sh4";
   char str5[] = "jaga12dish34";
   char str6[] = "j1a2g3a4d5i6s7h8";
   char str7[] = "1234";
   strip_off_chars(str1);
   printf("The stripped string is %s\n", str1);
   strip_off_chars(str2);
   printf("The stripped string is %s\n", str2);
   strip_off_chars(str3);
   printf("The stripped string is %s\n", str3);
   strip_off_chars(str4);
   printf("The stripped string is %s\n", str4);
   strip_off_chars(str5);
   printf("The stripped string is %s\n", str5);
   strip_off_chars(str6);
   printf("The stripped string is %s\n", str6);
   strip_off_chars(str7);
   printf("The stripped string is %s\n", str7);
}

OutPut:

The stripped string is 1234
The stripped string is 1234
The stripped string is 1234
The stripped string is 1234
The stripped string is 1234
The stripped string is 12345678

The stripped string is 1234

answer Feb 5, 2016 by Jagadish Nagaraddi
Similar Questions
+2 votes

Remove duplicate characters from given string?

Input: "cutcopypaste"
Output: "uoyase"

0 votes

Given a string, add some characters to the from of it so that it becomes a palindrome.
e.g.
1) If input is "abc" then "bcabc" should be returned.
2) input -> "ab" output -> "bab"
3) input -> "a" output -> "a"

+2 votes

Write a C program which accept two strings and print characters in second string which are not present in first string?

Example:
String 1: apple
String 2: aeroplane

output:
ron

...