Function returns only letters of string

user3478972

I wrote a program that should take a string and then return a string with only letters from the original one, but it's not working and can't figure out why. Can someone help me?

#include<stdio.h>

char *only_letters(char *s1ptr, char *s2ptr)
{
    while(*s1ptr)
    {
        if((*s1ptr >= 'a' && *s1ptr <= 'z') || (*s1ptr >= 'A' && *s1ptr <= 'Z'))
        {
            *s2ptr = *s1ptr;
            s2ptr++;
        }
        s1ptr++;
    }
    *s2ptr = '\0';
    return s2ptr;
}

int main()
{
    char *s1ptr, *s2ptr;
    printf("\n Write a string:\n");
    scanf("%s", s1ptr);
    printf("\n%s\n", *only_letters(s1ptr, s2ptr));
    return 0;
}
masoud

You have 3 problems.

  1. s1ptr and s2ptr are two unallocated arrays which will be caused to undefined behavior.

  2. You're incrementing s2ptr to the point which has value \0. After returning it you will have a string that just has \0.

  3. To show a string, you must pass it without * (don't dereference it).

Try this

char *only_letters(char *s1ptr, char *s2ptr_)
{
    char *s2ptr = s2ptr_;
    while(*s1ptr)
    {
        if((*s1ptr >= 'a' && *s1ptr <= 'z') || (*s1ptr >= 'A' && *s1ptr <= 'Z'))
        {
            *s2ptr = *s1ptr;
            s2ptr++;
        }
        s1ptr++;
    }
    *s2ptr = '\0';

    return s2ptr_;
}

int main()
{
    char s1ptr[256]; // Long enough
    char s2ptr[256]; // Long enough
    printf("\n Write a string:\n");
    scanf("%s", s1ptr);
    printf("\n%s\n", only_letters(s1ptr, s2ptr));
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

write a function which accepts a string and returns a new string with only the capital letters passed to the string

function that returns the number of letters that repeat in a string

ctypes - function only returns empty string

Write a function that takes a string parameter and returns a new string with all the letters of the alphabet that are not in the argument string

Function that takes a string representing a filename as an argument. Function returns number of letters in the txt file

Check if a string contains only letters

How to count only letters in string?

Define a function that only accepts a string with letters and only with one space between words

How to write a function solution in Java that given a String consisting of letters 'a' or/and 'b' returns true when occurences of 'a' are before 'b'?

Function takes a string and returns an object of the letters and number of occurrences -javascript - Homework Warning

Writing a function that returns different combinations of letters

Matlab returns the function only

Splitting string into 2 substrings: one with only letters and one with letters/numbers

Regex to match a string with 2 capital letters only

Get only letters in a string INCLUDING whitespace

Check if user put in only letters into String

Check if a string contains only letters, digits and underscores

Checking to see if a string is letters + spaces ONLY?

Check if string contains only letters in javascript

Check if a string contains only a set of letters

Only show first letters of string of each word

CSS: Display only the first two letters of a string

Verifying that a string contains only letters in C#

Printing only the letters by ABC order from String

Verifying that a string contains only letters in C++

Checking if string is only letters and spaces - Python

Javascript test string for only letters and numbers

How to remove only letters from a string in BigQuery?

How to test string for only letters or apostrophes?

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive