Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)
C Program : Remove All Characters in String Except Alphabets
Latest :

C Program : Remove All Characters in String Except Alphabets

C program to remove all Characters in a String Except Alphabet – When given a problem statement before directly jumping into solution, we need to first understand the problem statement thoroughly and then, also note down if any constraints are mentioned. Next, we have to determine the inputs necessary to arrive at a solution along with the output to expect at the end.

Later, we come up with the logical solution to the given problem statement so that, we can get our desired output using the inputs.

The given problem statement is, to write a C program to remove all characters except alphabets in a given string. To do so initially we require a string whose non-alphabetic characters are to be removed as our input. In the end, the output we desire is also a string where, there are only alphabets from the input string.

So firstly, we have to read our input string (c) at runtime. To do so, we can use gets() function which reads entire line input and stores it in a string pointer or character array. This reads input until it reaches either a newline or end-of-file (EOF).

char c[150];

printf(“Enter a string : “);

gets(c);

Now, we traverse through the character array (String) until we reach the end (‘\0’) in a loop. If a character is an alphabet then, it can either be uppercase or lowercase. Uppercase alphabets have ASCII values in the range 65-90 (‘A’-‘Z’) while, lowercase alphabets are in the range 97-122(‘a’-‘z’). If a character is in either one of these ranges then, we retain it in the character array (c) at index j (initially zero) and increment the value by 1.

The same happens to every character in the given string or character array. Once we reach the end of iteration, at the last index j we place the null value or end value i.e.,’\0′, marking the end of string.

for(i = 0,j=0;c[i] != ‘\0’; ++i) {

if((c[i]>=65 && c[i]<=90)||(c[i]>=97 && c[i]<=122)) {

c[j++]=c[i];

}

}

c[j]=’\0′;

This last value after the end of iteration in the string or character array (c) is nothing but, our resultant output string with no other characters except for the alphabets for a given input string. This value is then displayed on the console screen using printf().

printf(“String after removal : %s”,c);

Remove All Characters in A String Except Alphabets

Output:

Using Function

As we can observe above, the entire code is within main method. When we write the code that way, we can not make it reusable and also the code is not quite readable. Instead, if we use functions and split code accordingly then we can reuse it and enhance the readability.

This way, if the same logic is required somewhere else in the code then, we can just make function call and make it reusable. Also, code is more clean and if we need to find a particular part of code, we can directly go to the respective function instead of searching the entire main method.

Here, the main method will read the input string (c) using gets() function. Then, it calls the user-defined function (Remove) and passes the input string as parameter. This user-defined function (Remove) uses the same logic as discussed above to remove all the non-alphabetic characters in the parameter passed.

Then, this newly updated string that is our desired output is displayed on the console screen. Therefore, the main method handles all the necessary i/o operations for this problem statement while, the user-defined function (Remove) has the logic for removing non-alphabetic character.

Output:

x

Check Also

C Program To Check Whether A Number Is Even Or Odd | C Programs

C program to check whether a number is even or odd – In this article, ...

atakoy escortatakoy escortbakırköy escortşişli escortbağcılar escortsightcaredeneme bonusu veren siteleristanbul, escortromabetdeneme bonusu veren sitelerdeneme bonusu veren sitelerDeneme Bonusu Veren SitelerDeneme Bonusu Veren SitelerDeneme Bonusu Veren SitelerDeneme Bonusu Veren Sitelertaraftarium24deneme bonusu veren sitelergrandpashabetgrandpashabetGrandpashabetsloticadeneme bonusu veren sitelerdeneme bonusu veren sitelercasibom girişonwinvaycasinobets10bahcesehir masaj salonucasibom güncel girişdeneme bonusu veren sitelerdeneme bonusu veren sitelerdeneme bonusu veren sitelerdeneme bonusu veren sitelercasinofastDeneme bonusu veren sitelercasinofastdeneme bonusugrandpashabetdeneme bonusu veren sitelermarsbahis girişbetciomeritkingdeneme bonusu veren sitelerdeneme bonusu veren sitelerdeneme bonusu veren sitelerdeneme bonusu veren sitelerdeneme bonusu veren sitelerromabetistanbul escortgrandpashabetgrandpashabetsekabetgrandpashabetgrandpashabetramadabetmeritkingsekabetMeritkingcasibom girişmeritkinggrandpashabetroyalbetgrandpashabetdedebetcasibom girişcasibomcasibomcasibom girişcasibom girişpadişahbetMeritkinggrandpashabettrendbetjojobet girişonwin girişbets10royalbetgrandpashabetmatbetcasibom girişmarsbahis girişimajbetmeritking girişjojobet girişpusulabetpusulabet girişgrandpashabet girişholiganbetholiganbetroyalbetcasibomgalabetcasibommeritking girişcasibomcasibom girişcasibom girişcasibombetnanomegabahishacklink satın alsuperbetmeritking girişbetparkbetebet girişpusulabetasyabahis girişsuperbetinpiabet girişpiabet girişcratosslotpiabetodeonbet girişcoinbar girişperabetodeonbetsuperbetcoinbarvaycasino girişvaycasino güncel girişvaycasinovaycasino girişvaycasino güncel girişcasibomcasibom girişcasibom güncel girişcasibomcasibom girişcasibom güncel girişgrandpashabetgrandpashabet girişgrandpashabetgrandpashabet girişperabetasyabahisbetgray güncel girişvdcasinograndpashabetgrandpashabetgrandpashabetgrandpashabetgrandpashabetMeritkingzbahiscasibom girişcasibomcasibom girişroyalbet girişbets10vdcasino girişmatbetpusulabetdeneme bonusu veren sitelerBetpas GirişvdcasinoDeneme Bonusu Veren Sitelerjojobetbahiscasinobahiscasino girişjojobetcasibombahiscasino girişCasibom güncel girişDinamobet güncel girişbahiscasinopusulabetHoliganbetzbahis girişkralbetnakitbahis girişnakitbahisotobet girişotobetkralbet girişkralbetmeritking토토사이트betzulabetzula girişadana escortkulisbetvdcasino girişjojobetjojobetlotobetBetorder Girişcasibom güncel girişcasibombetasusbetparkcasibom girişcasibom girişcasibom girişücretsiz vpn indirsuperbet girişkalebetkalebet girişcasibom güncel girişBetpuancasibom