☠️
smadi0x86 Playground
  • 💀Welcome to smadi0x86 Playground
    • 🍷Resources
    • 🚬Projects
    • 🎓Certifications
    • 📌Pinned
    • ❓Questions
    • 📞Contact
  • 🏞️Cloud Native
    • Docker
      • Quick Reference
      • Introduction
      • Containers
      • Images
      • Storage & Volumes
      • Security
      • Cheatsheet
    • Git
    • Serverless Framework
    • YAML
  • 🔨Software Engineering
    • System Design
    • Environment Variables
    • JSON Web Tokens
  • 👾Architecture
    • C Language
      • Introduction
      • Calling Conventions
      • GCC Compilation
      • Libraries & Linking
      • I/O
      • Files
      • Pointers
      • Dynamic Memory Allocation
      • Data Types
      • Strings Manipulation
      • Bit Manipulation
      • Pre-processors
      • Macros
      • Type Qualifiers
    • C/C++ Build Systems
      • Fundamentals for Linking
      • Symbolic Linking
      • Cross-Platform Compilation
      • CMake for Building and Linking
      • Shared Libraries
      • Dynamic Linking and Dependency Management
    • Operating Systems
      • OS & Architecture
      • Processes
      • CPU Scheduling
      • Memory Management
  • 🛩️Cyber Warfare
    • Flight Physics
    • Communication
      • PWM & PPM
      • MAVLink
  • 🏴‍☠️Offensive Security
    • Active Directory
      • Introduction
    • Web Attacks
      • Server Side
        • OS Command Injection
        • Information Disclosure
        • Directory Traversal
        • Business Logic
        • Authentication
        • File Upload
        • SSRF
      • Client Side
        • CSRF
        • XSS
    • Recon
      • Active
        • Host discovery
        • Nmap
        • Mass Scan
      • Passive
        • Metadata
      • Web Applications
        • Discovery
        • Subdomains & Directories
        • SSL Certs
        • CMS
        • WAF Detection
      • Firewall Evasion
  • Binary Exploitation
    • Stack Smashing
      • x86
      • x86_64
    • pwntools
      • Processes and Communication
      • Logging and Context
      • Cyclic
      • Packing
      • ELF
      • ROP
  • 😈Advanced Persistent Threat
    • C2
      • Sliver
    • Malware
      • Windows Internals
        • PEB
      • Academy
        • Basics
      • Sektor7
        • Essentials
  • 💌Certifications
    • AWS Certified Cloud Practitioner (CLF-C01)
      • Cloud Foundations
      • Domain 1: Cloud Concepts
      • Domain 2: Security and Compliance
      • Domain 3: Technology
      • Domain 4: Billing and Pricing
    • AWS Certified Solutions Architect - Associate (SAA-C03)
      • Foundation
    • Certified Kubernetes Administrator (CKA)
      • Core Concepts
      • Scheduling
      • Logging & Monitoring
      • Application Lifecycle Management
      • Cluster Maintenance
      • Security
      • Storage
      • Networking
      • Design Kubernetes Cluster
      • Kubernetes The Kubeadm Way
      • Troubleshooting
      • JSONPATH
      • Lightning Lab
      • Mock Exams
      • Killer Shell
    • Certified Kubernetes Security (CKS)
      • Foundation
      • Cluster Setup
      • Cluster Hardening
      • Supply Chain Security
      • Runtime Security
      • System Hardening
      • Killer Shell
    • (KGAC-101) Kong Gateway Foundations
      • Introduction to APIs and API Management
      • Introduction to Kong Gateway
      • Getting Started with Kong Enterprise
      • Getting Started with Kong Konnect
      • Introduction to Kong Plugins
  • 📜Blog Posts
    • Modern Solutions For Preventing Ransomware Attacks
Powered by GitBook
On this page
  • String representation
  • Reversing a string
  • Analyzing strings
  • Finding mixed character strings (without string.h)
  • Summary
  1. Architecture
  2. C Language

Strings Manipulation

String representation

char word[7] = {"hellow"}; → always use double quotes and char type.

or

char word[7] = "hellow"; → can remove brackets.

or

char word[] = "hellow"; → this one is better.

Reversing a string

#include <stdio.h>
#include <string.h>

void reverse(char*, int, int);

int main()
{
    char a[100];
    
    gets(a);
    reverse(a, 0, strlen(a)-1);
    
    printf("%s\n", a);
    
    return 0;
}

void reverse(char *x, int begin, int end)
{
    char c;
    if (begin >= end)
        return;
        
    c = *(x+begin);
    *(x+begin) = *(x+end);
    *(x+end)   = c;
    
    reverse(x, ++begin, --end);
}

Analyzing strings

Example:

#include <stdio.h>
#include <ctype.h>

int main(){

    char a[100] = "hello world";

    for (int i = 0; a[i] != '\0'; i++ ){
        if(islower(a[i]))
            printf("lower: %c\n",a[i]);
    }


    return 0;
}

Finding mixed character strings (without string.h)

#include <stdio.h>
#include <stdbool.h>

int main(){

    char str[] = "Hello World";
    int i;
    char  found_lower = false, found_upper = false;

    for (int i = 0; str[i] != '\0'; i++) {
        found_lower = found_lower || (str[i] >= 'a' && str[i] <= 'z');
        found_upper = found_upper || (str[i] >= 'A' && str[i] <= 'Z');

        if (found_lower && found_upper) break;
    }

    printf("%d",found_lower && found_upper);

    return 0;
}

Summary

strlen(a)                → get the length of a

strncat(a,b,sizeOfA)     → append b to a 

strchr(a,'a')            → search for first occurence of 'a' in  string a

strrchr(a,'a')           → search for last occurence of 'a' in string a

strstr(a,b)              → search for string b in a

strncmp(a,b,sizeOfA)     → compare strings a and b

strncpy(a,b,sizeOfA)     → copy b to a (overwrite a)

memcmp(a,b,FirstNBytesToCompare) → compare first n bytes

memchr(a,'a',sizeOfA)  → search array a for character 'a'

strtok(a,t)            → tokenize string a by string t
PreviousData TypesNextBit Manipulation

Last updated 8 months ago

👾