Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C Language Examples - Remove Special Characters from a String

C Language Examples - Remove Special Characters from a String

C Language Examples C Language Examples

Remove all characters from a string except letters.

Example

#include

<

stdio.h

int

main

(

)

{

char

line

[

150

]

;

int

i

,

j

;

printf

(

"

Enter a string:

"

)

;

fgets

(

line

,

(

sizeof

line

/

sizeof

line

[

0

]

)

,

stdin

)

;

for

(

i

=

0

;

line

[

i

]

!= '\

0

'; ++

i

)

{

while

(

!

(

(

line

[

i

]

= '

a

' &&

line

[

i

]

<= '

z

'

)

||

(

line

[

i

]

= '

A

' &&

line

[

i

]

<= '

Z

'

)

||

line

[

i

]

== '\

0

'

)

)

{

for

(

j

=

i

;

line

[

j

]

!= '\

0

'; ++

j

)

{

line

[

j

]

=

line

[

j

1

]

;

}

line

[

j

]

= '\

0

';

}

}

printf

(

"

Output:

"

)

;

puts

(

line

)

;

return

0

;

}

The output is:



输入一个字符串: run4#$1oob

输出: runoob

C Language Examples C Language Examples