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?

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

Matlab returns the function only

Writing a function that returns different combinations of letters

Function returns boolean not string

The function returns empty string

Function that only returns false returns nothing

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

Checking if string is only letters and spaces - Python

Verifying that a string contains only letters in C++

Javascript test string for only letters and numbers

How to remove only letters from a string in BigQuery?