Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with strings

Status
Not open for further replies.

neopleasureline

Programmer
Nov 28, 2006
1
US
I'm a beginner and I'm having problems with some exercises. A few pointers in the right direction would be appreciated.

How do I count the number of tabs in a string? of words?

How do I switch the characters in a string with other characters?

 
C string is an array of char with zero byte at the end.
If you know C loops (for loop, for example) and array indexing it's so simple...
 
Use strlen() to get the length of the string.
Loop from 0 to strlen() and compare or swap the characters that you want.
 
You read every character in the string in a loop and check with the following condition for TAB
tab=0;
if(ch=='\10')
tab++;
For word check Kernighan Ritchie Book the program is given there
 
That's very bad programming for 2 reasons.
1. You've just obfuscated your code, since most people don't have the ASCII table memorized.
2. You've just thrown code portability out the window by making the assumption that the code will only be used on an ASCII based system.

Use '\t' for a tab character, '\n' for new line, '\r' for carriage return, ' ' for a space...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top