0% found this document useful (0 votes)
31 views12 pages

reverse String in C

The document contains C code snippets for reversing a string, sorting characters in a string, counting vowels in a string, swapping two strings, checking if two strings are anagrams, reversing words in a line, and reversing a line. The code uses functions like strlen(), printf(), strcat() while manipulating strings through indexing, loops and conditional statements.

Uploaded by

Nitin Abrol
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views12 pages

reverse String in C

The document contains C code snippets for reversing a string, sorting characters in a string, counting vowels in a string, swapping two strings, checking if two strings are anagrams, reversing words in a line, and reversing a line. The code uses functions like strlen(), printf(), strcat() while manipulating strings through indexing, loops and conditional statements.

Uploaded by

Nitin Abrol
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

//Reverse String in C

#include​ ​<stdio.h>

int​ main​()​ ​{
​char​ str1​[]​ ​=​ ​"Girish"​;​ ​// String 1
​char​ str2​[​8​];​ ​// Variable to store reverse string
​int​ length ​=​ ​0​;
​int​ length2 ​=​ ​0​;
​while​(​str1​[​length​]​ ​!=​ ​'\0'​)​ ​{
length2​++;
​}
printf​(​"\nPrinting in reverse - "​);
​for​(​length2 ​=​ ​--​length​;​ length2​>=​0​;​ length2​--)
printf​(​"%c"​,​ str1​[​length2​]);
length2 ​=​ ​0​;
printf​(​"\nStoring in reverse - "​);
​while​(​length ​>=​ ​0​)​ ​{
str2​[​length​]​ ​=​ str1​[​length2​];
length​--;
length2​++;
​}
str1​[​length2​]​ ​=​ ​'\0'​;​ ​// Terminates the string
printf​(​"%s\n"​,​ str2​);
​return​ ​0​;
}
Sort String Characters in C
​ stdio.h>
#include​ <
#include​ <​ string.h>

int​ main ​(​void​)​ ​{


​char​ ​str​[]​ ​=​ ​"girish"​;
​char​ temp​;
​int​ i​,​ j​;
​int​ n ​=​ strlen​(​string​);
printf​(​"String before sorting - %s \n"​,​ ​str​);

​for​ ( ​ ​i ​=​ ​0​;​ i ​<​ n​-​1​;​ i​++)​ ​{


​for​ ​(​j ​=​ i​+​1​;​ j ​<​ n​;​ j​++)​ { ​
​if​ ​(​str​[​i​]​ ​>​ ​str​[​j​])​ ​{
temp ​=​ ​str​[​i​];
​str​[​i​]​ ​=​ ​str​[​j​];
​str​[​j​]​ ​=​ temp​;
​}
​}
​ }
printf​(​"String after sorting - %s \n"​,​ ​str​);
​return​ ​0​;
}
Program to count vowels
#include​ ​<stdio.h>

int​ main​()​ ​{
​char​ s​[]​ ​=​ ​"girish"​;​ ​// String Given
​int​ i ​=​ ​0​;
​int​ vowels ​=​ ​0​;​ ​// Vowels count
​int​ consonants ​=​ ​0​;​ ​// Consonants count

​while​(​s​[​i​++]​ ​!=​ ​'\0'​)​ ​{


​if​(​s​[​i​]​ ​==​ ​'a'​ ​||​ s​[​i​]​ ​==​ ​'e'​ ​||​ s​[​i​]​ ​==​ ​'i'​ ​||​ s​[​i​]​ ​==​ ​'o'​ ​||
s​[​i​]​ ​==​ ​'u'​ ​)
vowels​++;
​else
consonants​++;
​}
printf​(​"'%s' contains %d vowels and %d consonants."​,​ s​,​ vowels​,
consonants​);
​return​ ​0​;
}
//Swapping of two Strings in C
#include​ ​<stdio.h>

int​ main​()​ ​{
​char​ str1​[]​ ​=​ ​"Girish"​;
​char​ str2​[]​ ​=​ ​"PavanTejas"​;
​char​ ch​;
​int​ index ​=​ ​0​;
​//Character by Character approach
printf​(​"Before Swapping - \n"​);
printf​(​"Value of str1 - %s \n"​,​ str1​);
printf​(​"Value of str2 - %s \n"​,​ str2​);
​while​(​str1​[​index​]​ ​!=​ ​'\0'​)​ ​{
ch ​=​ str1​[​index​];
str1​[​index​]​ ​=​ str2​[​index​];
str2​[​index​]​ ​=​ ch​;
index​++;
​}
printf​(​"After Swapping - \n"​);
printf​(​"Value of str1 - %s \n"​,​ str1​);
printf​(​"Value of str2 - %s \n"​,​ str2​);
​return​ ​0​;
}
//String Anagram Program in C
​ stdio.h>
#include​ <
#include​ <​ string.h>

int​ main ​(​void​)​ ​{


​char​ s1​[]​ ​=​ ​"listen"​;
​char​ s2​[]​ ​=​ ​"silent"​;

//fried , fired
//triangle integral
//heart, earth
//race, care
//part, trap
​ har​ temp​;
c

​int​ i​,​ j​;


​int​ n ​=​ strlen​(​s1​);
​int​ n1 ​=​ strlen​(​s2​);

​ / If both strings are of different length, then they are not


/
anagrams

​if​(​ n ​!=​ n1​)​ ​{


printf​(​"%s and %s are not anagrams! \n"​,​ s1​,​ s2​);
​return​ ​0​;
​ }

​// lets sort both strings first −

​for​ ( ​ ​i ​=​ ​0​;​ i ​<​ n​-​1​;​ i​++)​ ​{


​for​ ​(​j ​=​ i​+​1​;​ j ​<​ n​;​ j​++)​ ​{
​if​ ​(​s1​[​i​]​ ​>​ s1​[​j​])​ ​{
temp ​=​ s1​[​i​];
s1​[​i​]​ ​=​ s1​[​j​];
s1​[​j​]​ ​=​ temp​;
​}
​if​ ​(​s2​[​i​]​ ​>​ s2​[​j​])​ ​{
temp ​=​ s2​[​i​];
s2​[​i​]​ ​=​ s2​[​j​];
s2​[​j​]​ ​=​ temp​;
​}
​}
​ }

​// Compare both strings character by character


​for​(​i ​=​ ​0​;​ i​<​n​;​ i​++)​ ​{
​if​(​s1​[​i​]​ ​!=​ s2​[​i​])​ ​{
printf​(​"Strings are not anagrams! \n"​,​ s1​,​ s2​);
​return​ ​0​;
​}
​ }

printf​(​"Strings are anagrams! \n"​);


​return​ ​0​;
}
//C Program to reverse words in a line

​ stdio.h>
#include​ <
#include​ <​ string.h>

int​ string_length​(​char​ s​[])​ ​{


​int​ i ​=​ ​0​;
​while​(​s​[​i​]!=​'\0'​)
i​++;
​return​ i​;
}

void​ string_reverse​(​char​ st​[])​ ​{


​int​ i​,​j​,​len​;
​char​ ch​;

​ ​ len ​=​ string_length​(​st​)​ ​-​ ​1​;


j =
i =​ ​ ​0​;

​while​(​i ​<​ j​)​ ​{


ch ​=​ st​[​j​];
st​[​j​]​ ​=​ st​[​i​];
st​[​i​]​ ​=​ ch​;
i​++;
j​--;
​ }
}

int​ main ​(​void​)​ ​{


​char​ line​[]​ ​=​ ​"Strings are very topics in C"​;
​char​ reverse​[​100​]=​""​,​temp​[​50​];
​int​ i​,​j​,​n​;

n ​=​ string_length​(​line​);

​for​(​i ​=​ ​0​;​ i ​<​ n​;​ i​++)​ ​{

​for​(​j ​=​ ​0​;​ i ​<​ n ​&&​ line​[​i​]!=​' '​;​ ​++​i​,++​j​)​ ​{


temp​[​j​]​ ​=​ line​[​i​];
​}

temp​[​j​]​ ​=​ ​'\0'​;

string_reverse​(​temp​);
strcat​(​reverse​,​ temp​);
strcat​(​reverse​,​ ​" "​);
​}

printf​(​"Original - %s\n"​,​ line​);


printf​(​"Reversed - %s\n"​,​reverse​);

​return​ ​0​;
}
C Program to reverse a line
​ stdio.h>
#include​ <
#include​ <​ string.h>

int​ string_length​(​char​ s​[])​ ​{


​int​ i ​=​ ​0​;
​while​(​s​[​i​]!=​'\0'​)
i​++;
​return​ i​;
}

void​ string_reverse​(​char​ st​[])​ { ​


​int​ i​,​j​,​len​;
​char​ ch​;
j ​=​ len ​=​ string_length​(​st​)​ - ​ ​ ​1​;
i ​=​ ​0​;
​while​(​i ​<​ j​)​ ​{
ch ​=​ st​[​j​];
st​[​j​]​ ​=​ st​[​i​];
st​[​i​]​ ​=​ ch​;
i​++;
j​--;
​}
}

int​ main ​(​void​)​ ​{


​char​ line​[]​ ​=​ ​"Strings are very easy topics in C"​;
​char​ reverse​[​100​]​ ​=​ ​""​,​ temp​[​50​];
​int​ i​,​ j​,​ n​;
n ​=​ string_length​(​line​);

​for​(​i ​=​ n​-​1​;​ i ​>=​ ​0​;​ ​--​i​)​ ​{


​for​(​j ​=​ ​0​;​ i ​>=​ ​0​ ​&&​ line​[​i​]​ ​!=​ ​' '​;​ ​--​i​,++​j​)
temp​[​j​]​ ​=​ line​[​i​];

temp​[​j​]​ ​=​ ​'\0'​;

string_reverse​(​temp​);

strcat​(​reverse​,​temp​);
strcat​(​reverse​,​" "​);
​}

printf​(​"Original - %s\n"​,​ line​);


printf​(​"Reversed - %s\n"​,​reverse​);

​return​ ​0​;
}

You might also like