Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C++ Data Types
  • C++ Input/Output
  • C++ Arrays
  • C++ Pointers
  • C++ OOPs
  • C++ STL
  • C++ Interview Questions
  • C++ Programs
  • C++ Cheatsheet
  • C++ MCQ
  • C++ Projects
  • C++ Exception Handling
  • C++ Memory Management
Open In App
Next Article:
C++ Program to Sort String of Characters
Next article icon

C Program to Extract Characters From a String

Last Updated : 19 Jul, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

Character extraction can be done by iterating through the string in the form of a character array. It basically means plucking out a certain amount of characters from an array or a string.

Now, to take input in C we do it by using the following methods:

  • scanf("%c",&str[i]); - Using a loop
  • scanf("%s",str);  - Using %s format specifier
  • gets(str); - Gets gives us an extra ability to use space in an character string/array

Now the next step would be running the loop until we reach the null pointer, which exists in character arrays to indicate the end of the string. Being cautious, we will check for no blank space, if it is a valid character we print it using ("%c ",str[i]) else we will continue.

Example:

C
// C Program to demonstrate character  // extraction from a string #include <stdlib.h> #include <stdio.h> int main() {     char str[20];     printf("Enter the string: ");     gets(str);     // alternative scanf("%s",str);        // character extraction     printf("Printing the characters:: \n");     for (int i = 0; str[i] != '\0'; i++) {         if (str[i] != ' ') { // not a white space             printf("%c\n", str[i]); // printing each characters in a new line         }     }     return 0; } 

Input:

GeeksforGeeks

Output:

Enter the string: GeeksforGeeks Printing the characters::  G e e k s f o r G e e k s

Time Complexity: O(n) where n is the size of the string.

Auxiliary Space: O(1)


Next Article
C++ Program to Sort String of Characters

I

iamsuman898
Improve
Article Tags :
  • C++ Programs
  • C++
Practice Tags :
  • CPP

Similar Reads

  • How to Extract a Substring from a Character Array in C++?
    In C++, character arrays are used to store sequences of characters also known as strings. A substring is a part of a string that consists of a continuous sequence of characters from the string. In this article, we will learn how to extract a substring from a character array in C++. Extract a Substri
    2 min read
  • C++ Program to Sort String of Characters
    Sorting a string means rearranging the characters of the given string in some defined order such as alphabetical order. In this article, we will learn how to sort a string by characters in C++. Examples Input: str = "geeksforgeeks"Output: "eeeefggkkorss"Explanation: The characters in the string are
    4 min read
  • How to Remove Last Character From C++ String?
    In C++, strings are stored as the std::string class objects. In this article, we will look at how to remove the last character from this C++ string object. For Example, Input: Hello! GeeksOutput: Hello! GeekRemove the Last Character from a String in C++To remove the last character from a string, we
    2 min read
  • Program to find the kth character after decrypting a string
    Given a string str consisting of characters and numbers and an integer k, the task is to decrypt the string and the return the kth character in the decrypted string.In order to decrypt the string, traverse the string character by character and if the current character is an alphabet then append it t
    8 min read
  • Convert character array to string in C++
    This article shows how to convert a character array to a string in C++. The std::string in c++ has a lot of inbuilt functions which makes implementation much easier than handling a character array. Hence, it would often be easier to work if we convert a character array to string. Examples: Input: ch
    4 min read
  • How to Convert a Single Character to String in C++?
    A string is a generally made up of a sequence of multiple characters. In this article, we will learn how to convert a single character to a string in C++. Example: Input: c = 'A'Output: s = "a"Explanation: Character c is converted to a string. Input: c = '#'Output: s = "#"Explanation: Character c is
    4 min read
  • Remove odd frequency characters from the string
    Given string str of size N, the task is to remove all the characters from the string that have odd frequencies. Examples: Input: str = "geeksforgeeks" Output: geeksgeeks The characters f, o, r have odd frequencies So, they are removed from the string. Input: str = "zzzxxweeerr" Output: xxrr Approach
    9 min read
  • C++ Program to Print the First Letter of Each Word of a String
    String str is given which contains lowercase English letters and spaces. It may contain multiple spaces. Get the first letter of every word and return the result as a string. The result should not contain any space. Examples: Input: str = "geeks for geeks" Output: gfg Input: str = "happy coding" Out
    4 min read
  • How to Read a File Character by Character in C++?
    In C++, file handling is used to store data permanently in a computer. Using file handling we can store our data in secondary memory (Hard disk). In this article, we will learn how to read a file character by character in C++. Example: Input: "Geeks for Geeks"Output: G e e k s f o r G e e k sRead Te
    2 min read
  • Extract and print words separately from a given Camel Case string
    Given a string in Camel Case format, we need to extract all the words which are present in the string. CamelCase is the sequence of one or more than one words having the following properties: It is a concatenation of one or more words consisting of English letters.All letters in the first word are l
    6 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences