How to put dot(.) in a string in C?


 
Thread Tools Search this Thread
Top Forums Programming How to put dot(.) in a string in C?
# 1  
Old 11-26-2012
How to put dot(.) in a string in C?

Hi all,

Can anybody please tell me how can I put dot(.) in a string using C programming.
for example --
I am having string "10111988" and I want to convert it as "10.11.1988"

I will appriciate and thanks in advance..
# 2  
Old 11-26-2012
Hi

Code:
#include<stdio.h>
#include<string.h>

int main()
{
        char a[11]="10111988";
        char b[11];
        int i,j=0,len;
        printf ("The input is :<%s>\n", a);
        len=strlen(a);
        for(i=0;i<len;i++){
                b[j++]=a[i];
                if (i==1 || i==3) {
                        b[j++]='.';
                }
        }
        b[j]='\0';
        printf ("The o/p string is <%s>\n",b);
        return 1;
}


On running this:
Code:
The input is :<10111988>
The o/p string is <10.11.1988>

# 3  
Old 11-26-2012
Hi ,
I am getting input from string data[0].first.last(10 char) and when I am trying to apply above code,
Quote:
char a[11]=data[0].first.last;
and after running, I am getting input as null value.

Quote:
The input is :< >
Thanks in advance..
# 4  
Old 11-26-2012
Hi
You need to use strcpy to copy strings:

Code:
strcpy(a,data[0].first.last);


Guru.
# 5  
Old 11-26-2012
SEGV waiting to happen:

Code:
        char a[11]="10111988"; // 10 chars, fits in 11 bytes with terminating NUL.
        char b[11];            // 11 bytes can't hold 12 chars plus terminating NUL

And

Code:
strcpy(a,data[0].first.last);

Without data validation, another potential SEGV. Or potential security hole.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to split timestamp and put a dot between YYYYMMDD and HHMMSS?

I have a string time=20170303201234 I want to split it and put a dot result: 20170303.201234 CODE: ttdotss=`echo ${time} | {8}.{8}` Doesn't understand I tried this: CODE: ttdotss=`echo ${time} |cut -c 1-8 | . | cut -c 9-14` Result: script: .: argument expected... (4 Replies)
Discussion started by: digioleg54
4 Replies

2. Shell Programming and Scripting

How to put dot in timestap?

I have a string: Code time=20170303122334 I need the result: 20170303.122334 I did: CODE: ttdotss=`echo ${time} |sed 's\(.|{8\}\)/|1 /g'` Result sed: Function s\(.|{8\}\)/|1 /g cannot be parsed. Could you please help me to resolve this issue? Thanks for contribution (2 Replies)
Discussion started by: digioleg54
2 Replies

3. Shell Programming and Scripting

sed - search and replace whole string which contains dot

Hello. I would like to search exactly "string1.string2.string3" and replace it by "new_string1.new_string2.new_string3" And I would like to search exactly "string2.string3" and replace it by "new_string2.new_string3" And I would not found in the result : "string1.new_string2.new_string3"... (3 Replies)
Discussion started by: jcdole
3 Replies

4. Shell Programming and Scripting

Put a string to the beginning of a file without a linefeed

Hello, I need to put the following string to the beginning of a file - but it should not create a newline at the end of the string. So, the file I have is a compressed one - with gzip. And I would like to add an ASCII-String to the beginning of the file. The string has a length of 69... (5 Replies)
Discussion started by: API
5 Replies

5. Shell Programming and Scripting

How to use sed to put string end of line?

I have several file as below, and i want to put .txt to specific text contain ^main=EXE^cmd=run script /usr/prog/bd_, file1 7.9102 12.1528 16.3672 7.4002 ^main=EXE^cmd=run script /usr/prog/bd_123^" line 16.3672 7.3134 17.8711 6.0981 file 2 7.9102 12.1528 16.3672 7.4002 ... (8 Replies)
Discussion started by: zulabc
8 Replies

6. Shell Programming and Scripting

put string end of the line

I've a problem to put .h end of the line..below my input file fg_a bb fg_b bb fg_c bb fg_d aa fg_f ee and i want the output file as below fg_a.h bb fg_b.h bb fg_c.h bb fg_d.h (6 Replies)
Discussion started by: zulabc
6 Replies

7. Shell Programming and Scripting

Regular Expression doesn't match dot "." in a string

hello, I am writting a regular expression that intend to match any tunnel or serial interface but it doesn't mtach any serial sub-interface. For example, statement should match "Tunnel3" or "Serial0/1" but shouldn't match "Serial0\1.1" (doesn't include dot ".") I tried the following but... (3 Replies)
Discussion started by: ahmed_zaher
3 Replies

8. Shell Programming and Scripting

How to compare particular string, if it is equal put into a new file

Hi, I have a file (sample.txt) this file contains below text. ./au ./11.5.0 ./11.5.0/admin ./11.5.0/admin/driver ./po ./11.5.0 ./11.5.0/admin ./11.5.0/admin/driver ./xxsbx/11.5.0/java/src ./xxsbx/11.5.0/lib ./xxsel ./xxsel/11.5.0 ./xxsel/11.5.0/admin ./zfa ./zfa/11.5.0... (2 Replies)
Discussion started by: gagan4599
2 Replies

9. Shell Programming and Scripting

Curious question? How to put a string into two columns.

Now I have a list of numbers in hand and I try to put the numbers into two columns. Can I do this work with any script? Great thanks to your help! 1A1.log HF=-240.451527 HF=-240.5213996 1A2.log HF=-240.451527 HF=-240.5213996 1B.log HF=-240.4273718 HF=-240.4956636 1C.log... (7 Replies)
Discussion started by: liuzhencc
7 Replies

10. Shell Programming and Scripting

put a string before a searched string

hi all! i have a working that looks like this... file1 MALE JOHN MALE ANJO FEMALE ANNE MALE JAMES FEMALE HONEY FEMALE IZA what i want to do is insert "M" when the first string is "MALE" and insert "F" when the first string is "FEMALE". file1 M MALE ... (10 Replies)
Discussion started by: kingpeejay
10 Replies
Login or Register to Ask a Question