Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Questions on C strings

Tags:

c

string

I am new to C and I am very much confused with the C strings. Following are my questions.

Finding last character from a string

How can I find out the last character from a string? I came with something like,

char *str = "hello";
printf("%c", str[strlen(str) - 1]);
return 0;

Is this the way to go? I somehow think that, this is not the correct way because strlen has to iterate over the characters to get the length. So this operation will have a O(n) complexity.

Converting char to char*

I have a string and need to append a char to it. How can i do that? strcat accepts only char*. I tried the following,

char delimiter = ',';
char text[6];
strcpy(text, "hello");
strcat(text, delimiter);

Using strcat with variables that has local scope

Please consider the following code,

void foo(char *output)
{
   char *delimiter = ',';
   strcpy(output, "hello");
   strcat(output, delimiter);
}

In the above code,delimiter is a local variable which gets destroyed after foo returned. Is it OK to append it to variable output?

How strcat handles null terminating character?

If I am concatenating two null terminated strings, will strcat append two null terminating characters to the resultant string?

Is there a good beginner level article which explains how strings work in C and how can I perform the usual string manipulations?

Any help would be great!

like image 592
Navaneeth K N Avatar asked Jun 30 '10 09:06

Navaneeth K N


People also ask

What is Strings in C with example?

In C programming, a string is a sequence of characters terminated with a null character \0 . For example: char c[] = "c string"; When the compiler encounters a sequence of characters enclosed in the double quotation marks, it appends a null character \0 at the end by default.

What are C strings used for?

Strings are used for storing text/characters. For example, "Hello World" is a string of characters. Unlike many other programming languages, C does not have a String type to easily create string variables.

What are the types of string in C?

They are int , short , long , float , double , long double and char . As you see, there is no built-in string or str (short for string) data type.

How does C represent strings?

String constants A string constant in C is represented by a sequence of characters within double quotes. Standard C character escape sequences like \n (newline), \r (carriage return), \a (bell), \0x17 (character with hexadecimal code 0x17), \\ (backslash), and \" (double quote) can all be used inside string constants.

How are C strings stored?

Overview. The C language does not have a specific "String" data type, the way some other languages such as C++ and Java do. Instead C stores strings of characters as arrays of chars, terminated by a null byte.


2 Answers

  1. Last character: your approach is correct. If you will need to do this a lot on large strings, your data structure containing strings should store lengths with them. If not, it doesn't matter that it's O(n).

  2. Appending a character: you have several bugs. For one thing, your buffer is too small to hold another character. As for how to call strcat, you can either put the character in a string (an array with 2 entries, the second being 0), or you can just manually use the length to write the character to the end.

  3. Your worry about 2 nul terminators is unfounded. While it occupies memory contiguous with the string and is necessary, the nul byte at the end is NOT "part of the string" in the sense of length, etc. It's purely a marker of the end. strcat will overwrite the old nul and put a new one at the very end, after the concatenated string. Again, you need to make sure your buffer is large enough before you call strcat!

like image 194
R.. GitHub STOP HELPING ICE Avatar answered Sep 17 '22 13:09

R.. GitHub STOP HELPING ICE


  1. O(n) is the best you can do, because of the way C strings work.
  2. char delimiter[] = ",";. This makes delimiter a character array holding a comma and a NUL Also, text needs to have length 7. hello is 5, then you have the comma, and a NUL.
  3. If you define delimiter correctly, that's fine (as is, you're assigning a character to a pointer, which is wrong). The contents of output won't depend on delimiter later on.
  4. It will overwrite the first NUL.

You're on the right track. I highly recommend you read K&R C 2nd Edition. It will help you with strings, pointers, and more. And don't forget man pages and documentation. They will answer questions like the one on strcat quite clearly. Two good sites are The Open Group and cplusplus.com.

like image 22
Matthew Flaschen Avatar answered Sep 20 '22 13:09

Matthew Flaschen